Get Rows Count from sqlite database in iPhone ios
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;}