Reputation: 3243
In my iPhone app, I want to backup/restore/delete some data to/from SQLite3, but I'm experiencing an anoying issue.
If I do a clean build of the app, it will succesfully take my data, put it in the DB, and then delete when needed (DB used for backing up data before sending to server, and then deleting once transfer is completed).
BUT, after this first round (I press home button, and build/debug again), the DB gets locked for some reason? I'm using sqlite_exec to do the INSERT and DELETE queries, so human error should be minimum right?
Below is the code I use (And yes I know the sql stmt is butt ugly):
//SAVING
-(void)gemRegIDb:(Registrering *)reg
{
//SAVE REGISTRATION TO DB
sqlite3 *db = [[FotoDokAppDelegate shared] database];
char *err;
NSString *sql = [NSString stringWithFormat:@"INSERT INTO Registrering (regId, imagePath, name, date, sentCount, status,igang, latStr, lngStr, brugerID, projektID, felter, harGPS, tempBilledeNavn) Values('%@','%@','%@','%@','%i','%@','%i','%@','%@','%@','%@','%@','%i','%@');",reg.regId,reg.imagePath,reg.name,reg.date,reg.sentCount,reg.status,reg.igang,reg.latStr,reg.lngStr,reg.brugerID,reg.projektID,reg.felter,reg.harGPS,reg.tempBilledeNavn];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
{
NSLog(@"Noget gik galt i GEM REGISTRERING TIL DB.");
}
}
//DELETING:
-(void)sletRegFraDb:(Registrering *)reg
{
//DELETE REGISTRATION FORM DB
sqlite3 *db = [[FotoDokAppDelegate shared] database];
char *err;
NSString *sql = [NSString stringWithFormat:@"DELETE FROM Registrering WHERE regId ='%@'",reg.regId];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
{
NSLog(@"Noget gik galt i SLET REGISTRERING TIL DB.");
}
}
After second run the err variable is = "database is locked".
EDIT: I thought that removing the exit(0); and inserting some sqlite3_close(db); in some places worked, but it did not :(
The flow of my problem:
Can anyone help me?
Upvotes: 0
Views: 687
Reputation: 14113
Make sure that your SQLite browser is not executing any SQLite query. For that matter, you should once close your SQLite browser while running the application to make sure this is the possible reason. I faced the same problem couple of days ago and did the same. Luckily I came out of this problem.
Upvotes: 1