如果注释部分保持注释,则代码运行良好。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**