Reputation: 3
I have the following code n i have 4 records in data base but loop is running 2 times n getting only first row two times whats a problem in my code?
- (void) getAllRowsFromTableName:(NSString *)tableName{
NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM '%@'",tableName];
NSLog(@"query is :%@",qsql);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db,[qsql UTF8String] , -1, &statement,nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSLog(@"loop");
int catId = sqlite3_column_int(statement, 1);
NSLog(@"***CatId is :%d",catId);
sqlite3_finalize(statement);
}
sqlite3_finalize(statement);
}
Upvotes: 0
Views: 95
Reputation: 8564
Just to complete and close this question I am putting my comment in answer.
Try with commenting the sqlite3_finalize(statement); inside the while loop. Explaination:
SQLite.org says
The sqlite3_finalize() function is called to delete a prepared statement.
Also,
The application must finalize every prepared statement in order to avoid resource leaks. It is a grievous error for the application to try to use a prepared statement after it has been finalized. Any use of a prepared statement after it has been finalized can result in undefined and undesirable behavior such as segfaults and heap corruption.
Thanks,
Upvotes: 0
Reputation: 8131
Wow! Another that use sqllite3 natively!! :)
I would suggest you an external lib (2 files) named FMDB, that is a wrapper to sqllite3
.
Easy to use. Hope this helps.
Upvotes: 2