我是 Objective-C 和 iphone 应用程序的新手。
我正在访问 SQLite,我的表“coffee”中有 3 行。我使用以下方法从表中取出某物,但是,只有第 2 行和第 3 行被拉出{第 1 行总是被遗漏}。这是由于我的 while 循环中的逻辑通过检查 while sqlite3_step(selectstmt) 返回 SQLITE_ROW 是错误的吗?这是代码:
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select coffeeID, coffeeName from coffee";
sqlite3_stmt *selectstmt;
NSLog(@"sqlite_prepare_v2 returns: %i", sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL));
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
NSLog(@"sqlite3_step returns: %i", sqlite3_step(selectstmt));
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.coffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(@"this is the coffee name: %@", coffeeObj.coffeeName);
coffeeObj.isDirty = NO;
[appDelegate.coffeeArray addObject:coffeeObj];
[coffeeObj release];
}
}
}
另一方面,有什么方便的方法可以让我直接从 SQLite 的 C 接口(interface)检查查询中返回的行数?
非常感谢。
最佳答案
您可以使用查询 SELECT COUNT(*) FROM coffee
来告诉您有多少行。
而且,省去一些头痛和use a SQLite wrapper .
关于objective-c - 从 Objective-C 中的 SQLite C 接口(interface)获取行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865615/