Bạn cần thêm tệp SQLite vào dự án Xcode của mình trước tiên - địa điểm thích hợp nhất là trong thư mục tài nguyên. Sau đó, trong ứng dụng của bạn đại biểu tập tin mã, trong phương pháp appDidFinishLaunching, trước tiên bạn cần phải kiểm tra xem một bản sao có thể ghi của tập tin SQLite đã được tạo ra - tức là: một bản sao của tập tin SQLite đã được tạo ra trong người sử dụng thư mục tài liệu trên hệ thống tệp của iPhone. Nếu có, bạn không làm gì cả (nếu không bạn sẽ ghi đè lên nó bằng bản sao Xcode SQLite mặc định)
Nếu không, sau đó bạn sao chép tệp SQLite ở đó - để ghi nó.
Xem ví dụ dưới đây để thực hiện việc này: điều này đã được lấy từ mẫu mã sách SQLite của Apple, nơi phương thức này được gọi từ ứng dụng đại biểu phương thức appDidFinishLaunching.
// 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]);
}
}
============
Dưới đây là các mã trên trong Swift 2.0+
// Creates a writable copy of the bundled default database in the application Documents directory.
private func createEditableCopyOfDatabaseIfNeeded() -> Void
{
// First, test for existence.
let fileManager: NSFileManager = NSFileManager.defaultManager();
let paths:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory:NSString = paths.objectAtIndex(0) as! NSString;
let writableDBPath:String = documentsDirectory.stringByAppendingPathComponent("bookdb.sql");
if (fileManager.fileExistsAtPath(writableDBPath) == true)
{
return
}
else // The writable database does not exist, so copy the default to the appropriate location.
{
let defaultDBPath = NSBundle.mainBundle().pathForResource("bookdb", ofType: "sql")!
do
{
try fileManager.copyItemAtPath(defaultDBPath, toPath: writableDBPath)
}
catch let unknownError
{
print("Failed to create writable database file with unknown error: \(unknownError)")
}
}
}
bạn có thể làm hài lòng trang từ nơi bạn lấy mã không? Cảm ơn. – itsaboutcode
Cơ sở dữ liệu ở đâu nếu bạn muốn viết trên đó? Tôi biết rằng nếu nó thuộc nhóm Resourses (Main Bundle) thì đó là chỉ đọc, vì vậy đó là cách phù hợp để cho phép cả hai, viết và đọc? Cảm ơn! –
Tôi biết điều này hơi trễ một chút, nhưng làm cách nào để tìm hiểu xem nó có cần cập nhật từ gói chính không? Tôi đã thử kiểm tra các thuộc tính tập tin, nhưng ngày sửa đổi của tập tin trong ứng dụng thay đổi bất cứ khi nào tôi biên dịch một phiên bản mới. – Flipper