0

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);
    }
2
  • Try with commenting the sqlite3_finalize(statement); inside the while loop. Commented Mar 1, 2012 at 14:22
  • yes u r right by commenting the sqlite3_finalize(statement); inside the while loop my code is now working fine Thanks Ravin. Commented Mar 1, 2012 at 14:30

2 Answers 2

2

Wow! Another that use sqllite3 natively!! :)

I would suggest you an external lib (2 files) named FMDB, that is a wrapper to sqllite3.

https://github.com/ccgus/fmdb

Easy to use. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.