【发布时间】:2011-08-24 04:02:37
【问题描述】:
我正在尝试使用以下代码来计算我的 SQLite 数据库表中的行数,但它会引发异常。这是一种更简单的方法吗?
- (void) countRecords {
int rows = 0;
@try {
NSString *dbPath = [self getDBPath];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *strSQL;
strSQL = @"SELECT COUNT(*) FROM MYTABLE";
const char *sql = (const char *) [strSQL UTF8String];
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {
// THIS IS WHERE IT FAILS:
if (SQLITE_DONE!=sqlite3_step(stmt) ) {
NSAssert1(0,@"Error when counting rows %s",sqlite3_errmsg(database));
} else {
rows = sqlite3_column_int(stmt, 0);
NSLog(@"SQLite Rows: %i", rows);
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
}
}
@catch (NSException * e) {
NSLog(@"Error Counting");
}
}
【问题讨论】:
-
数据库“内部”的表的名称是什么?
-
对不起。我含糊其辞。我的意思是计算表中的行数,而不是数据库。
标签: objective-c sqlite count