问题描述
我是新来的Objective-C和iPhone应用程序。
I am new to objective-C and iphone apps.
我访问SQLite和在我的表咖啡的3行。我用下面的方法来从表抢了某事,但是,只有第二和第三行被拉出{第一行始终无缘}。是,由于通过检查而sqlite3_step(selectstmt)返回SQLITE_ROW是错误的,我while循环逻辑?这里是code:
I am accessing SQLite and have 3 rows in my table "coffee". I used the following way to grab sth out from the table, however, only then 2nd and 3rd rows are being pulled out {the 1st row is always missed}. Is that due to the logic in my while loop by checking while sqlite3_step(selectstmt) returns SQLITE_ROW is wrong? Here is the code:
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接口检查在查询行returen数任何方便的方式?
On the other hand, is there any convenient way for me to check the number of rows returen in a query directly from the C interface of SQLite?
非常感谢。
推荐答案
您可以使用来自咖啡查询 SELECT COUNT(*)
来告诉你有多少行有
You could use the query SELECT COUNT(*) FROM coffee
to tell you how many rows there are.
和也,保存自己有些头痛和。
And also, save yourself some headaches and use a SQLite wrapper.
这篇关于获取的行数从SQLite的C接口在Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!