开发者

sqlite: can i do select statement while looping through another select statement

开发者 https://www.devze.com 2022-12-16 01:18 出处:网络
In my iPhone app, I use a sqlite table to hold my data. While I\'m looping through a select statement on one of the tables, I want to do another select 开发者_如何学编程(and update) statement on the s

In my iPhone app, I use a sqlite table to hold my data. While I'm looping through a select statement on one of the tables, I want to do another select 开发者_如何学编程(and update) statement on the same table in some conditions. It's pretty complicated to explain why I would want to do this, but I'm pretty sure I need to.

The problem is that when I loop through the outer while loop, if I call the inner select statement, it terminates the outer loop after that run, even if it has more rows in the sql statement. So is this impossible? Can I call a select statement while I'm looping through the sqlite3_step results on the same table? Here's some psuedo-code (in objective-c) to explain what I'm doing:

sqlite3_exec(database, "BEGIN", 0, 0, 0); // Begin Transaction

    if (init_all_statement == nil)
    {
        const char *sql = "SELECT id, fullname FROM contact";

        if (sqlite3_prepare_v2(database, sql, -1, &init_all_statement, NULL) != SQLITE_OK) {
            NSAssert1(0, @"Error Test.m: failed with message '%s'.", sqlite3_errmsg(database));
        }
    }

    while (sqlite3_step(init_all_statement) == SQLITE_ROW)
    {
        ...

        if (blah) 
        {
            if (get_duplicate_rows == nil)
            {
                const char *sql = "SELECT id, fullname FROM contact where fullname = ?";

                if (sqlite3_prepare_v2(database, sql, -1, &get_duplicate_rows, NULL) != SQLITE_OK) {
                    NSAssert1(0, @"Error Test.m: failed with message '%s'.", sqlite3_errmsg(database));
                }
            }

            sqlite3_bind_text(get_duplicate_rows, 1, [contact_fullname UTF8String], -1, SQLITE_TRANSIENT);

            while (sqlite3_step(get_duplicate_rows) == SQLITE_ROW)
            {
                ...
            }
            sqlite3_reset(get_one_row);
        }

    }


Yes, it is possible, you only need to create a new statement to receive the new results

sqlite3_stmt *statement;

Cheers,
VFN

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号