while(sqlite3_step(statement)== SQLITE_ROW)从未执行过?

时间:2011-05-10 05:29:40

标签: iphone sql ios4

while(sqlite3_step(statement)== SQLITE_ROW)循环没有执行可以有人帮忙吗?

NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);输出始终为21 100

sqlite3_stmt *statement;
    sqlite3 *database;
if (sqlite3_open("myDB.sql", &database) == SQLITE_OK) {
    NSLog(@"1 %d",SQLITE_OK);
NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);       
        const char *sql = "SELECT myName FROM myTable";
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {   
                int primaryKey = sqlite3_column_int(statement, 0);

            }
        }

        if (statement) {
            sqlite3_finalize(statement);
        }
    } 

2 个答案:

答案 0 :(得分:1)

Try This,



sqlite3_stmt *statement;
        sqlite3 *database;
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"myDB" 
                ofType:@"sql"];

            if (sqlite3_open([sqLiteDb UTF8String], &database) != SQLITE_OK) {
                NSLog(@"Failed to open database!");
            }
            NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);       
            const char *sql = "SELECT myName FROM myTable";
            if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
                while (sqlite3_step(statement) == SQLITE_ROW) {   
                    int primaryKey = sqlite3_column_int(statement, 0);

                }
            }
            else {
                 sqlite3_close(database);
                 NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
            }

            if (statement) {
                sqlite3_finalize(statement);
            }
        }

这里有一些链接供参考。

答案 1 :(得分:0)

sqlite3 *database;

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    {   
    NSString *sqlStatement = [NSString stringWithFormat:@""SELECT myName FROM myTable"];
      sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding],-1,&compiledStatement, NULL) == SQLITE_OK) 
        {
            NSLog(@"%@",sqlStatement);

            if (sqlite3_step(compiledStatement) == SQLITE_ROW) 
            {
int primaryKey = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0 )]intValue] ;
            }       
        }
        sqlite3_finalize(compiledStatement);

        sqlite3_close(database);
    }