Until now all my database access has been reading, but now I need to update (and after this insert)
I have a database containing 'shows' in the app directory (read-only) and that's ok for me (I don't want to copy it to the documents folder as it's rather big and I don't need to change stuff in it.
But I want the user to select some shows as his favorite. Therefore I created a database with a table 'favorite_shows' in the documents folder. It contains 4 fields: ID (prim key) show_id is_favorite remarks (currently not yet in use)
The user is able to toggle the 'is_favorite' status ONCE, after that I'm getting an error when trying to update:
SQLITE_BUSY 5 /* The database file is locked */
This is my code:
if (sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
sqlStatement = "select * from favorite_shows WHERE (show_id = ?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, self.ID);
// search for a show
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// if we can find one, toggle the status
favID = sqlite3_column_int(compiledStatement, 0); // we need the primary key to update it
isFav = (sqlite3_column_int(compiledStatement, 2) == 1); // let's store the favorite status
sqlStatement = "update favorite_shows SET is_favorite = ? WHERE (ID = ?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, !isFav );
sqlite3_bind_int(compiledStatement, 2, favID);
int error = sqlite3_step(compiledStatement);
if (SQLITE_DONE != error) {
NSLog(@"error while updating favorite status");
}
}
}
//else : no records found indicating that this show hasn't been a favorite yet, so insert one as favorite
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
What is the reason that it's locked the second time ? Is there some other instruction to give besides :
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
to close everything ?