在SQLite数据库中保存FMDatabase
问题描述:
如何在我的项目中将我的FMDatabase数据库保存到SQLite数据库(.db)?在SQLite数据库中保存FMDatabase
我有一个TableView,我用FMDatabase显示我的Sqlite数据库(.db)。我用executeUpdate更改了该数据库,但我无法将这些更改应用到我的项目中原始的sqlite数据库(.db)。
我很不熟悉英语,原创教程对我来说很难。 我希望你能帮助我理解。
答
FMDB保存的数据库本身就是一个SQLite数据库。您可以使用FMDB打开您的Documents文件夹中的SQLite数据库(您可以从包中复制或者以编程方式创建)。你永远不会改变数据库中的数据库(如果你甚至有一个数据库)。
什么,你通常会做的是检查,看看是否在数据库中的文档存在,如果没有,有复制从包:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"db"];
NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:@"Base.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:documentsPath]) {
NSError *error;
BOOL success = [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error];
NSAssert(success, @"%s: copyItemAtPath error: %@", __FUNCTION__, error);
}
// now FMDB can use the database at `documentsPath`
数据库FMDB保存到_is_ SQLite数据库,所以我你不清楚你在问什么。您可以使用FMDB打开您的Documents文件夹中的SQLite数据库(您可以从包中复制或者以编程方式创建)。你永远不会改变数据库中的数据库(如果你甚至有一个数据库)。你能澄清你的问题吗? – Rob
明白了!我解决了我的问题。我只写了这些字符串: NSString * path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@“Base.db”]; NSString * path2 = [文件stringByAppendingPathComponent:@“Base.db”]; if(![[NSFileManager defaultManager] fileExistsAtPath:path2]){NSData * xmlData = [NSData dataWithContentsOfFile:path]; [xmlData writeToFile:path2 atomically:YES]; } – Ike
是的。我可能会建议使用'copyItemAtPath'而不是通过一个'NSData'来回翻它,但这是基本正确的。 – Rob