This one also bit me; it took me a whole morning going through FMDatabase and reading the sqlite3 API documentation to find it. I'm still not quite sure about the root cause of the problem, but according to this error in PHP , you need to call sqlite3_exec instead of preparing the statement with sqlite3_prepare_v2 and then calling sqlite3_step.
The documentation does not seem to suggest that this behavior will occur, hence our confusion, and I would like someone with more experience with sqlite to come up with some hypotheses.
I solved this by developing a method to execute a batch of queries. Please find the code below. If you prefer, you can rewrite this into a category rather than just adding it to FMDatabase.h, your call.
Add this to the FMDatabase interface in FMDatabase.h:
- (BOOL)executeBatch:(NSString*)sql error:(NSError**)error;
Add this to the FMDatabase implementation in FMDatabase.m:
- (BOOL)executeBatch:(NSString *)sql error:(NSError**)error { char* errorOutput; int responseCode = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &errorOutput); if (errorOutput != nil) { *error = [NSError errorWithDomain:[NSString stringWithUTF8String:errorOutput] code:responseCode userInfo:nil]; return false; } return true; }
Note that executeBatch lacks many functions that make it unsuitable for many purposes. In particular, it does not check if the database is locked, it does not guarantee that FMDatabase itself is not locked, it does not support caching of statements.
If you need it, the above is a good starting point for the code. Happy hack!
Steve rukuts
source share