2011-07-05 25 views
10

Tôi cần phải nhập khẩu một tập tin .sqlite vào Core Data, tôi đã tìm kiếm trên internet và tìm thấy:Làm cách nào để nhập tệp sqlite đã tồn tại trước vào Dữ liệu chính?

Core Data Tutorial: How To Preload/Import Existing Data

Nó đang tạo ra một kịch bản Python để cư cơ sở dữ liệu này bằng cách đọc trong các nội dung của cơ sở dữ liệu cũ của chúng tôi và tạo các hàng thích hợp trong cơ sở dữ liệu mới. Nhưng cơ sở dữ liệu sqlite của tôi quá lớn về số lượng các bảng và cột, điều này có thể khiến tôi mất một khoảng thời gian đáng kể.

Tôi cũng thấy điều này:

Using a Pre-Populated SQLite Database with Core Data on iPhone OS 3.0

Nhưng tôi không hoàn toàn hiểu nó, nó trông giống như nó được sao chép cơ sở dữ liệu cũ để một hình mới, sau đó như thế nào thêm Z_ hậu tố cho tất cả các bàn và tên cột? Ngoài ra, nó yêu cầu tôi để tạo ra các thực thể và các thuộc tính, có anyway này có thể được thực hiện tự động (từ tập tin sqlite dabase)?

Cảm ơn!

+0

Không, không có bất kỳ cách tự động. Tệp dabase Sqlite được tạo bên ngoài của Core Data không tương thích với nó. – alhcr

Trả lời

2

này trả lời ở đây có thể có ích (tôi là một trong số họ)

Pre-populate Core Data

/** 
Returns the path to the application's Documents directory. 
*/ 
     - (NSString *)applicationDocumentsDirectory { 
      return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
     } 

sample code

+0

Cảm ơn nhưng [self applicationDocumentsDirectory] stringByAppendingPathComponent dường như không tồn tại, tôi đang sử dụng XCode 4.3 – hzxu

+0

Ngoài ra, tôi có cần phải tạo các thực thể/thuộc tính trước khi thực hiện những gì được hiển thị trong mã của bạn không? Cảm ơn! – hzxu

+0

@hzxu rằng phương pháp chỉ đơn giản là một phương pháp tiện lợi, tôi vừa chỉnh sửa câu hỏi của tôi để đưa nó vào. –

0
// Returns the persistent store coordinator for the application. 
// If the coordinator doesn't already exist, it is created and the application's store added to it. 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) 
    { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"yourSqlite.sqlite"]; 

    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"yourSqlite.sqlite" ofType:nil]; 

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSError *error = nil; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] ]) { 

     if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] error:&error]){ 
      NSLog(@"Default file successfully copied over."); 
     } else { 
      NSLog(@"Error description-%@ \n", [error localizedDescription]); 
      NSLog(@"Error reason-%@", [error localizedFailureReason]); 
     } 
    } 

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return _persistentStoreCoordinator; 
} 
Các vấn đề liên quan