0

Well i am new to iphone coding and using sqlite in one of my application the problem is my sqlite database does not updates the modified data, i am following a sqlite based tutorial:

this is what i am doing:-

- (void) saveAllData {

 if(isDirty) {


  if(updateStmt == nil) {
   const char *sql = "update Work Set Engagement = ?, Status = ?, Where WorkID = ?";
   if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK) 
    NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
  }

  sqlite3_bind_text(updateStmt, 1, [Engagement UTF8String], -1, SQLITE_TRANSIENT);
  sqlite3_bind_text(updateStmt, 2, [Status UTF8String], -1, SQLITE_TRANSIENT);
   //sqlite3_bind_double(updateStmt, 2, [Status doubleValue]);
  sqlite3_bind_int(updateStmt, 3, WorkID);
  //int Success = sqlit3_step(updateStmt);


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

  sqlite3_finalize(updateStmt);

  isDirty = NO;
 }

i am using :

- (IBAction) save_Clicked:(id)sender {


 //Update the value.
 //Invokes the set<key> method defined in the Work Class.
 [objectToEdit setValue:txtField.text forKey:self.keyOfTheFieldToEdit];
 [self.navigationController popViewControllerAnimated:YES];
}

for save button click and :

- (void) setEngagement:(NSString *)newValue {

 self.isDirty = YES;
 [Engagement release];
 Engagement = [newValue copy];
}

- (void) setStatus:(NSString *)newNumber {

 self.isDirty = YES;
 [Status release];
 Status = [newNumber copy];
}

for setting values, i banged my head thousand times and can not find what i am doing wrong can some body plz help me with this.....

this is the code for my getDBPath implementation

- (NSString *) getDBPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"CheckList.sqlite"];
}

this code for my Data Base : - (void) copyDatabaseIfNeeded {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CheckList.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success) 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
spider1983
  • 1,098
  • 1
  • 7
  • 14
  • What is the path to `database`? The most common reason for not saving is that the database file has not been moved to the user's documents directory. – falconcreek Sep 03 '10 at 13:14
  • possible duplicate: http://stackoverflow.com/questions/2785211/sqlite3-database-doesnt-actually-insert-data-iphone – falconcreek Sep 03 '10 at 13:19
  • i think i have done it as i can insert data in the database but can't modify it.. – spider1983 Sep 03 '10 at 14:18
  • this what i did .. in my code: - (void) copyDatabaseIfNeeded { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSString *dbPath = [self getDBPath]; BOOL success = [fileManager fileExistsAtPath:dbPath]; if(!success) { NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CheckList.sqlite"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; if (!success) NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); } } – spider1983 Sep 03 '10 at 14:20
  • Please update the question with your code instead adding as a comment. Add the implementation for `getDBPath` as well. – falconcreek Sep 03 '10 at 14:45
  • where do you call `-(void) saveAllData`? – falconcreek Sep 03 '10 at 14:55
  • i call it when the application is going to terminate in – spider1983 Sep 06 '10 at 09:05
  • - (void)applicationWillTerminate:(UIApplication *)application { [self.WorkArraymakeObjectsPerformSelector:@selector(saveAllData)]; – spider1983 Sep 06 '10 at 09:06

1 Answers1

0

Your code doesn't show opening the database, but I'm guessing you're opening one that is included in your app bundle? If so, you'll first need to copy the database into your app's Documents directory, because the application bundle is read-only.

You can get the path to the Documents directory with this:

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

Brian
  • 15,599
  • 4
  • 46
  • 63
  • yes i have done that as if i insert a new data that get saved in database but if i try to modify the existing data and then save it it doesn't works i get the same old data i was trying to modify... – spider1983 Sep 03 '10 at 14:04
  • i tried using break point and it breaks at: [objectToEdit setValue:txtField.text forKey:self.keyOfTheFieldToEdit]; – spider1983 Sep 03 '10 at 14:12