Sqlite inserts get lost after app restart

前端 未结 2 1234
刺人心
刺人心 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!!

提交回复
热议问题