从 iPhone ios 中的 sqlite 数据库获取行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5006488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Rows Count from sqlite database in iPhone ios
提问by Viktor Apoyan
Background
背景
All day long I've been trying to solve a problem, I read all the articles and documentation that I could find on the Internet, but I can't solve this. I'm writing an application for iPhone and I need to work with a sqlitedatabase (sqlite3).
一整天我都在试图解决一个问题,我阅读了互联网上可以找到的所有文章和文档,但我无法解决这个问题。我正在为 iPhone 编写应用程序,我需要使用sqlite数据库 ( sqlite3)。
Main Problem
主要问题
I have created my database and all is going good until I wanted to get a count of the rows in my table. The Table name is ARTICLES
, so I wrote
我已经创建了我的数据库并且一切顺利,直到我想获得我表中的行数。表名是ARTICLES
,所以我写了
SELECT COUNT(*) FROM ARTICLES
My program does nothing and writes in the log: Unknown Error.
我的程序不执行任何操作并写入日志:未知错误。
const char *query = "SELECT COUNT (*) FROM ARTICLES";
sqlite3_stmt *compiledQuery;
sqlite3_prepare_v2(database, query, -1, &compiledQuery, NULL);
Program gives message "Unknown Error" in the above code, and I can't get the count of rows. Who can help me to solve this problem... or may be something with sqlite is not correct?
程序在上面的代码中给出消息“未知错误”,我无法获得行数。谁能帮我解决这个问题......或者可能是sqlite的东西不正确?
Code
代码
- (int) GetArticlesCount
{
if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK)
{
const char* sqlStatement = "SELECT COUNT(*) FROM ARTICLES";
sqlite3_stmt *statement;
if( sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
{
if( sqlite3_step(statement) == SQLITE_DONE )
{
}
else
{
NSLog( @"Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(articlesDB) );
}
}
else
{
NSLog( @"Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(articlesDB) );
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(articlesDB);
}
return 0;
}
In this line the unknown error appears:
在这一行中,出现了未知错误:
NSLog( @"Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(articlesDB) );
What must I add to the code or what must I do to get the count of rows? Please help...
我必须在代码中添加什么或我必须做什么才能获得行数?请帮忙...
Working Code (Not effective)
工作代码(无效)
const char* sqlStatement = "SELECT * FROM ARTICLES";
sqlite3_stmt *statement;
if( sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
{
int count = 0;
while( sqlite3_step(statement) == SQLITE_ROW )
count++;
}
I get the right count of rows! But I don't think it is an effective method... I think that something with sqlite is not going right...
我得到了正确的行数!但我不认为这是一种有效的方法......我认为sqlite的东西不正确......
回答by theChrisKent
Thank you for the update, I believe the problem is your check against SQLITE_DONE
instead of SQLITE_ROW
, so I have updated your method below:
感谢您的更新,我相信问题是您的检查SQLITE_DONE
而不是SQLITE_ROW
,所以我在下面更新了您的方法:
- (int)getArticlesCount {
int count = 0;
if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) ==
SQLITE_OK) {
const char* sqlStatement = "SELECT COUNT(*) FROM ARTICLES";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(articlesDB, sqlStatement, -1, &statement, NULL) ==
SQLITE_OK) {
// Loop through all the returned rows (should be just one)
while(sqlite3_step(statement) == SQLITE_ROW) {
count = sqlite3_column_int(statement, 0);
}
} else {
NSLog(@"Failed from sqlite3_prepare_v2. Error is: %s",
sqlite3_errmsg(articlesDB));
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(articlesDB);
}
return count;
}