使用 iOS 应用程序中的现有数据 [英] Use existing data in iOS application
问题描述
我是 iOS 开发的新手,绝对不完全了解 Xcode
和其他东西的工作原理.我正在创建一个 iOS 应用程序并且一直使用 Sqlite
在设备上存储数据.我在项目中添加了一个 sqlite3
数据库,但我注意到在模拟器中数据库在应用程序第一次运行时不存在,因此必须在代码中创建.直到现在这都没问题.我应该有一份包含申请日期的清单.是否可以让应用程序在首次运行时使用已包含一些数据的数据库?
I'm pretty new to iOS development and definitely do not completely understand the workings of Xcode
and stuff. I am creating an iOS application and have been using Sqlite
to store data on the device. I had added a sqlite3
database to the project but I noticed that in the simulator the database does not exist at first run of the application and thus has to be created in code. This was ok until now. I am supposed to have a list with dates that should come with the application. Is it possible to have the application use a database with some data already in it at first run?
推荐答案
// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
这是在sqlite数据库不存在的情况下复制它..
This is to copy over the sqlite database if it does not exist..
希望这会有所帮助.
这篇关于使用 iOS 应用程序中的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!