iphone - SQLite count(*) 如何得到结果?

标签 iphone count sqlite

我已经完成了这段代码来计算数据库中的行数

    int rows = 0;
if (sqlite3_open([[SqliteManager getDBPath] UTF8String], &database) == SQLITE_OK) {
    const char *sql = "select count(*) from artheca";
    sqlite3_stmt *countstmt;

    if(sqlite3_prepare_v2(database, sql, -1, &countstmt, NULL) == SQLITE_OK) {              
        NSLog(@"inside");
        rows = sqlite3_column_int(countstmt, 0);
    }
}
else
    sqlite3_close(database);
return rows;

但结果总是0。

所以,我不确定 rows = sqlite3_column_int(countstmt, 0); 是否是获取行数的正确语句...是否正确?

最佳答案

编辑:要执行 SQL 语句,您需要 call all these 6 functions in order :

sqlite3_open()        Open the database
sqlite3_prepare()     Create the SQL statement
sqlite3_step()        Execute the statement
sqlite3_column()      Fetch the result
sqlite3_finalize()    Destroy the statement
sqlite3_close()       Close the database

您缺少 sqlite3_stepsqlite3_finalize 步骤。

顺便说一句,你可以使用 sqlite3_get_table()sqlite3_exec()对于这些简单的查询。


来自 http://www.sqlite.org/c3ref/column_blob.html ,

The leftmost column of the result set has the index 0.

因此你应该使用

rows = sqlite3_column_int(countstmt, 0);

关于iphone - SQLite count(*) 如何得到结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3116890/

相关文章:

php - COUNT操作前如何检查?

c - 在C中,使用while循环来计算用户输入的字符数

java - 从多个表中获取 count(*) 值

iphone - 整合 FlurryAds

iphone - 在后台状态下继续操作

iphone - 连接失败后调用 viewDidLoad 无效

bash - 如何使用 Bash 插入 SQLite 数据库?

php - 如何在我的 in php 应用程序中修复来自 sqlite 的这些警告?

mysql - 将查询从 MySql 转换为 Sqlite

iphone - 向 iOS 开发者帐户添加更多设备