1

I tried to update an entry in my table like this:

NSFileManager *fileMgr=[NSFileManager defaultManager];
        NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"quizDB.sqlite"];

        if ([fileMgr fileExistsAtPath:dbPath]) {
            NSLog(@"DB does exist!");//displayed
        }
        const char *dbpath = [dbPath UTF8String];
        sqlite3_stmt    *updateStmt;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            NSLog(@"%i",id_quiz);//displayed, in a test case it display 1 which is a quiz ID in the table
            NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz]; 
            const char*sql=[querySql UTF8String];

            sqlite3_prepare_v2(contactDB, sql, -1, &updateStmt, NULL);

                if(SQLITE_DONE != sqlite3_step(updateStmt))
                { 
                    NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB));
                }
                else{ 

                    sqlite3_reset(updateStmt);
                    NSLog(@"Update done successfully!");//this is also displayed, i guess everything is ok then and the entry is updated
                }

            sqlite3_finalize(updateStmt);           
            sqlite3_close(contactDB);
        }

In the log, i got the message: Update done successfully! so i assumed that everything is, however when i check the database within SQLite Manager in Mozilla Firefox, the entry is not updated. Am i missing something? thanx for help

Luca
  • 20,399
  • 18
  • 49
  • 70

1 Answers1

2

first copy your database from resource to document dir:

- (void) checkAndCreateDatabase
{
NSString *database_Name =@"quizDB.sqlite"
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *database_Path = [documentsDir stringByAppendingPathComponent:database_Name];

NSFileManager *fileManager = [NSFileManager defaultManager];

if(![fileManager fileExistsAtPath:database_Path])
{
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name];

    [fileManager removeItemAtPath:database_Path error:nil];
    [fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil];
}
else
{
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name];
    [fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil];
}

 }

and after you update your database:

 - (void) UpdateDatabase
{       
sqlite3 *contactDB;
sqlite3_stmt *updateStmt;
NSString *database_Name =@"quizDB.sqlite"

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbpath = [documentsDir stringByAppendingPathComponent:database_Name];

if(sqlite3_open([dbpath UTF8String], &contactDB) == SQLITE_OK)
  {
  NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz];
  const char*sql=[querySql UTF8String];
    if(sqlite3_prepare_v2(contactDB,sql, -1, &updateStmt, NULL) == SQLITE_OK)
    {
        if(SQLITE_DONE != sqlite3_step(updateStmt))
            {
                    NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB));
            }
            else{
                sqlite3_reset(updateStmt);
                NSLog(@"Update done successfully!");
                }
      }



    sqlite3_finalize(updateStmt);
}
sqlite3_close(contactDB);

 }

and check your database which save in documentdir not in resource  

Hector
  • 3,909
  • 2
  • 30
  • 46
  • Hi Piyush Patel: when and where should the `checkAndCreateDatabase ` gets called please? – Luca Mar 19 '12 at 12:17
  • Ok, what i did so far is that i put the `checkAndCreateDatabase` method in the `AppDelegate` file and called it in the `didFinishLaunchingWithOptions` method, however that didn't worked. My SQLite DB file is in the `Supporting Files` directory, what would you mean by `check your database which save in documentdir not in resource` Thanx for clarification :) – Luca Mar 19 '12 at 12:33
  • It doesn't work :( the entry is not updated, i didn't understand you when you said `check your database which save in documentdir not in resource`---My SQLite DB File currently is on the `Supporting Files` directory in Xcode, shouldn't it be there? – Luca Mar 19 '12 at 12:40
  • Under Application Support, there is no iPhone Simulator folder. – Luca Mar 19 '12 at 12:58
  • `/Users/Malek/Library/Application Support/iPhone Simulator/5.0/Applications/013G6556-2EF6-403A-B6CC-D4387FACCE41/Documents/quizDB.sqlite` – Luca Mar 19 '12 at 13:10
  • And what if i want to test the app on the device? – Luca Mar 19 '12 at 13:25
  • device has also that same path you don't need change – Hector Mar 19 '12 at 13:27
  • there db available or not tell me – Hector Mar 19 '12 at 13:27