2011-07-05 17 views
10

मैं कोर डेटा में एक .sqlite फ़ाइल आयात करने की जरूरत है, मैं इंटरनेट पर खोज की है और पाया: हमारे पुराने डेटाबेस की सामग्री में पढ़करकोर डेटा में प्री-मौजूदा स्क्लाइट फ़ाइल कैसे आयात करें?

Core Data Tutorial: How To Preload/Import Existing Data

यह इस डेटाबेस को भरने के लिए Python स्क्रिप्ट पैदा कर रही है , और नए डेटाबेस में उपयुक्त पंक्तियां बनाना। लेकिन टेबल और कॉलम की संख्या की अवधि में मेरा स्क्लाइट डेटाबेस बहुत बड़ा है, इससे मुझे काफी समय लग सकता है।

मैं भी इस पाया:

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

लेकिन मैं काफी नहीं समझते हैं कि यह ऐसा लगता है कि यह एक नया एक पुराने डेटाबेस को कॉपी है, तो यह कैसे Z_ प्रत्यय सभी तालिका में जोड़ करता है और कॉलम नाम? इसके अलावा, यह मुझे संस्थाओं और विशेषताओं को बनाने के लिए कहता है, वैसे भी यह स्वचालित रूप से किया जा सकता है (स्क्लाइट डाबेस फ़ाइल से)?

धन्यवाद!

+0

नहीं है, वहाँ किसी भी स्वचालित तरीका नहीं है। कोर डेटा के बाहर बनाई गई स्क्लाइट डाबेस फ़ाइल इसके साथ संगत नहीं है। – alhcr

उत्तर

2

यह यहाँ का जवाब देता उपयोगी हो सकता है (मेरा उनमें से एक है)

Pre-populate Core Data

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

sample code

+0

धन्यवाद लेकिन [स्वयं अनुप्रयोग दस्तावेज़ डायरेक्टरी] stringByAppendingPathComponent मौजूद प्रतीत नहीं होता है, मैं एक्सकोड 4.3 – hzxu

+0

का उपयोग कर रहा हूं, साथ ही, क्या मुझे आपके कोड में दिखाए जाने से पहले इकाइयों/विशेषताओं को बनाने की आवश्यकता है? धन्यवाद! – hzxu

+0

@hzxu यह विधि बस एक सुविधा विधि है, मैंने इसे शामिल करने के लिए अभी अपना प्रश्न संपादित किया है। –

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; 
} 
संबंधित मुद्दे