0

如果注释部分保持注释,则代码运行良好。while 循环应该运行两次,它确实导致线程表中只有两行。但是,如果我取消注释注释代码,while 循环只运行一次,这是错误的输出

sql = "select thread_id,timestamp from threads";

    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

        while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            NSString* abc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
            NSString* def = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

            NSLog(@"%@",abc);
            NSLog(@"%@",def);
            NSString* threadid = abc;
            NSString *sql2 = [NSString stringWithFormat:@"select * from my where t_id=\"%@\"",threadid];

**Commented code begin**               
            /* sqlite3_finalize(selectstmt);

            sqlite3_open([path UTF8String], &database);
                if(sqlite3_prepare_v2(database, [sql2 UTF8String], -1, &selectstmt2, NULL) == SQLITE_OK) {
                int a = sqlite3_data_count(selectstmt2);
                NSLog(@"%d",a);

                    if (a==1) {

                    sql2 = [NSString stringWithFormat:@"select timestamp from my where t_id=\"%@\"",threadid];
                        if(sqlite3_prepare_v2(database, [sql2 UTF8String], -1, &selectstmt2, NULL) == SQLITE_OK) {

                            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                            NSString* abc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt2, 0)];
                            NSLog(@"%@",abc);
                            }
                        }  
                    } 

                    if(a==0) {

                    char* error;
                    sql2 = [NSString stringWithFormat:@"insert into my(t_id,time) values(\"%@\",\"%@\")",threadid,@"0"];

                    int i = sqlite3_exec(database, [sql2 UTF8String], NULL, NULL, &error) ;
                    NSLog(@"inserted");
                    sqlite3_finalize(selectstmt);
                    }
              }*/

**Commented code end**

**program end**
4

1 回答 1

0

您正在完成准备好的语句,在您处理它的同一个循环中。毫不奇怪,下次您尝试使用该语句时,它会失败。请注意,您对语句进行的每个调用在完成后都会返回一个错误代码,您可以在继续执行其他逻辑之前检查该错误代码。

于 2012-12-29T14:50:26.330 回答