从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;
}
在此行中出现未知错误: em>
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...
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屋!