0
votes

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:

  • Open app
  • Do stuff
  • App backs up stuff to DB
  • App sends stuff to server, and deletes from DB afterwards.
  • That works, but if I kill the app and debug again, the db is now locked.
  • The app can read the content of db, but writes are locked.
  • Only solution is to remove app from simulator and re-install it.

Can anyone help me?

1
Hmm aparrently the guy who developed this app that I'm now in charge of is using exit(0); in the applicationDidEnterBackground method, and that seems to be the culprit. Deleting that, and using sqlite3_close(db); in a few places solves the problem - At least thats what it looks like for now :)David K

1 Answers

1
votes

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.