sqlite3 DELETE problem "Library Routine Called Out Of Sequence"
Here is my second stupid Noob problem. I am trying to do a simple Delete and I keep blowing up on the prepare step. I already have other Deletes, Inserts, Updates and Selects working. I am sure it is something simple. I appreciate your help.
+ (void)flushTodaysWorkouts {
sqlite3_stmt *statement = nil;
//open the database
if (sqlite3_open([[BIUtility getDBPath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, @"Failed to opendatabase");
}
NSArray *woList = [self todaysScheduledWorkouts];
for (Workout *wo in woList) {
NSInteger woID = wo.woInstanceID;
if(statement == nil) {
const char *sql = "DELETE FROM IWORKOUT WHERE WOINSTANCEID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while cr开发者_运维技巧eating delete statement. '%s'", sqlite3_errmsg(database));
}
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_int(statement, 1, woID);
if (SQLITE_DONE != sqlite3_step(statement))
NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
sqlite3_finalize(statement);
}
if(database) sqlite3_close(database);
}
I solved this, but I don't know why. It did not want to let me do the deletes inside of the for loop. I put the delete in it's own method and called it from a for loop and it worked great. Weird.
I have just come across this error message, and it turned out to be two threads executing queries simultaneously. Just rearranging the code was enough to change the timing so that it would go away.
Adding locks to prevent multiple accesses to the database resolved the problem
I've just encountered a similar one.
It turned out I forgot to reset a prepared and bound statement for DELETE, which would run into trouble the second time it got invoked.
For the case in this question, perhaps the sqlite3_finalize
should be replaced with a splite3_reset
to reuse in a loop block. And the statement should be finalized only after the loop before invoking sqlite3_close
.
精彩评论