从 iPhone ios 中的 sqlite 数据库获取行数 [英] Get Rows Count from sqlite database in iPhone ios
问题描述
我一整天都在试图解决一个问题,我阅读了互联网上可以找到的所有文章和文档,但我无法解决这个问题.我正在为 iPhone 编写应用程序,我需要使用 sqlite 数据库 (sqlite3).
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 sqlite database (sqlite3).
我已经创建了我的数据库并且一切顺利,直到我想获得表中的行数.表名是ARTICLES
,所以我写了
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
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);
程序给出消息未知错误";在上面的代码中,我无法获得行数.谁能帮我解决这个问题...或者可能是sqlite的东西不对?
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?
- (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;
}
在这一行中出现了未知错误:
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...
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++;
}
我得到了正确的行数!但我不认为这是一个有效的方法......我认为sqlite的东西不正确......
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_DONE
而不是 SQLITE_ROW
,所以我在下面更新了你的方法:
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:
- (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;
}
这篇关于从 iPhone ios 中的 sqlite 数据库获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!