Sqlite inserts get lost after app restart

前端 未结 2 1235
刺人心
刺人心 2021-01-16 08:00

This is the insert code. It works fine until i close the app and start it again, then all changes are gone. I don\'t see any error, could it be some iphone specific issue?

相关标签:
2条回答
  • 2021-01-16 08:10

    To write your DB to file manager:

    NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSString *dbPath = [self getDatabasePath];
        BOOL success = [fileManager fileExistsAtPath:dbPath]; 
    
        if(!success) {
    
            NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourDB.sqlite"];
            success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    
            if (!success) 
                NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    

    To fetch your DB from file manager:

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

    By this way your changes will be saved, next time you open your DB. I would rather suggest use FMDatabase wrapper, written in Obj C over sqlite. Here is the link for FMDatabase: http://code.google.com/p/flycode/source/browse/trunk/fmdb

    hope it helps!!

    0 讨论(0)
  • 2021-01-16 08:36

    Is the SQLite database file in the AppBundle, if so you will need to copy it to the document directory if you want to make changes. You can't write to any files in your app bundle.

    0 讨论(0)
提交回复
热议问题