Sqlite总是只返回1行

时间:2014-04-27 17:00:31

标签: ios sqlite

计数返回3,因为我在列中有3行。但是当我尝试发布名称时,它只返回1行。

请看一下我的代码。

  sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_chatDB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:
                          @"SELECT MESSAGE from CHATCOMPLETE"];
    const char *query_stmt = [querySQL UTF8String];
    if (sqlite3_prepare_v2(_chatDB,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
         if (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 0)];
                           NSLog(@"Name is %@",name);
            }

        }
        else{
            NSLog(@"Not found");
         //   return nil;
        }
        sqlite3_reset(statement);
    }
}

1 个答案:

答案 0 :(得分:3)

将您的if/else更改为while以逐步浏览所有行:

while (sqlite3_step(statement) == SQLITE_ROW) {
        NSString *name = [[NSString alloc] initWithUTF8String:
                          (const char *) sqlite3_column_text(statement, 0)];
                       NSLog(@"Name is %@",name);
}

然后您可以处理所有结果。

您应该将sqlite3_reset更改为sqlite3_finalize。并且不要忘记关闭数据库。

相关问题