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?