SQLite数据库有时会删除所有表

时间:2014-03-12 00:52:03

标签: ios objective-c sqlite fmdb

我的应用程序有一个SQLite数据库。我不知道如何解释,但有时sqlite文件中的所有表都会被删除。它不会经常发生,但它会发生。 .db文件仍然存在,但没有表。

我使用此代码打开并从文档目录中获取.db。

#pragma mark LOCAL DATABASE
-(void)createEditableCopyOfDatabaseIfNeeded {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Spread.sqlite"];
    BOOL success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) {
        return;
    }
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Spread.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    } else {
        NSLog(@"Spread - DB Created");
    }
}
+(FMDatabase *) getDatabase {
    static FMDatabase *db;
    if (!db) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Spread.sqlite"];
        FMDatabase *db = [FMDatabase databaseWithPath:writableDBPath];
        [db open];
        return db;
    } else {
        return db;
    }
}

0 个答案:

没有答案