Reputation: 21
I am developing an iOS app using sqlite. This is a very data-centric app so I have made a function called prepareQuery. I won't ever be accessing the database at multiple times, so cmd is global and is working great.
-(BOOL) prepareQuery:(NSString *) query
{
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &db) == SQLITE_OK)
{
const char *query_stmt = [query UTF8String];
if (sqlite3_prepare_v2(db, query_stmt, -1, &cmd, NULL) == SQLITE_OK)
return YES;
}
return NO;
}
Now I have the following (edited for readability)
[self prepareQuery:@"SELECT STATEMENT FOR TABLE 1;"];
NSLog(@"Result: %i", sqlite3_step(cmd));
//Work with the 1st query
sqlite3_finalize(cmd);
[self prepareQuery:@"SELECT STATEMENT FOR TABLE 2;"]];
NSLog(@"Result: %i", sqlite3_step(cmd));
//Work with the 2nd query
sqlite3_finalize(cmd);
if ([self prepareQuery:@"INSERT STATEMENT FOR TABLE 1;"])
NSLog(@"Result: %i", sqlite3_step(cmd));
else
NSLog(@"error preparing statement");
I get the following output:
Result: 100
Result: 100
Result: 5
This is not the first procedure I've had where I've ran multiple selects then done an insert. Any idea why the last result is SQLITE_BUSY? I've tried closing the database, and setting cmd to nil.
Upvotes: 1
Views: 182
Reputation: 21
I found the issue (I knew that would happen as soon as I hit post...)
The code I posted is called from a different procedure. All 15 statements in there were finalized except one. I finalized that one and it worked. I don't understand why I could run 7 selects, 1 update, 7 more selects without finalizing #4 but it gave an error in the 2nd procedure...
Upvotes: 1