2009-04-04 7 views
32

जब मैं प्रारंभ में अपने आवेदन के लिए पूर्व-डाले गए डेटासेट के साथ एक SQLite डेटाबेस फ़ाइल बनाता हूं, तो मुझे अपने फ़ाइल को अपने एक्सकोड प्रोजेक्ट में कहीं भी रखना होगा ताकि यह मेरे आईफोन एप्लिकेशन पर जा सके। मुझे लगता है कि "ressources" उस के लिए सही जगह है।आप एक आईफोन ऐप में अपनी SQLite डेटाबेस फ़ाइल कहां रखेंगे?

आईफोन एप्लिकेशन में SQLite डेटाबेस फ़ाइल की तैनाती के लिए बुनियादी "कदम" क्या हैं?

  • डेटाबेस मैन्युअल
  • बनाने परियोजना के लिए डेटाबेस फ़ाइल जोड़ने (कहाँ?)

मैं वर्तमान में पूरे SQLite प्रलेखन पढ़ रहा हूँ, हालांकि वह ज्यादा iPhone संबंधित नहीं।

उत्तर

70

आपको पहले अपने एक्सकोड प्रोजेक्ट में SQLite फ़ाइल जोड़ने की आवश्यकता है - सबसे उपयुक्त स्थान संसाधन फ़ोल्डर में है।

आपके आवेदन प्रतिनिधि कोड फ़ाइल में

फिर, appDidFinishLaunching विधि में, आपको पहले जांच करने की आवश्यकता SQLite फ़ाइल की एक लिखने योग्य प्रति पहले से ही बनाया गया है - यानी: SQLite फ़ाइल की एक प्रतिलिपि उपयोगकर्ताओं में बनाया गया है आईफोन की फाइल सिस्टम पर दस्तावेज़ फ़ोल्डर। यदि हां, तो आप कुछ भी नहीं करते हैं (अन्यथा आप इसे डिफ़ॉल्ट एक्सकोड SQLite प्रतिलिपि के साथ ओवरराइट करेंगे)

यदि नहीं, तो आप वहां SQLite फ़ाइल की प्रतिलिपि बनाते हैं - इसे लिखने के लिए।

ऐसा करने के लिए नीचे दिए गए कोड उदाहरण देखें: यह ऐप्पल के SQLite पुस्तक कोड नमूने से लिया गया है जहां इस विधि को एप्लिकेशन प्रतिनिधि एपडिडफिनिश लॉन्चिंग विधि से कहा जाता है।

// 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]); 
    } 
} 

============

यहाँ स्विफ्ट 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)") 
     } 
    } 
} 
+0

क्या आप कोड प्राप्त कर सकते हैं, जहां से आप कोड प्राप्त कर सकते हैं? धन्यवाद। – itsaboutcode

+0

यदि आप इसे लिखना चाहते हैं तो डेटाबेस कहां होना चाहिए? मुझे पता है कि अगर यह रिसोर्स समूह (मुख्य बंडल) में है तो यह केवल पढ़ने के लिए है, तो दोनों लिखने और पढ़ने की अनुमति देने का सही तरीका कौन सा है? धन्यवाद! –

+0

मुझे पता है कि यह थोड़ा देर हो चुकी है, लेकिन मैं यह जानने के लिए कैसे जाउंगा कि इसे मुख्य बंडल से अपडेट करने की आवश्यकता है या नहीं? मैंने फ़ाइल विशेषताओं की जांच करने का प्रयास किया, लेकिन जब भी मैं एक नया संस्करण संकलित करता हूं तो ऐप में फ़ाइल की संशोधन तिथि बदल जाती है। – Flipper

6

में उपरोक्त कोड है तुम सिर्फ डेटा के लिए क्वेरी जा करने के लिए जा रहे हैं, आप इसे मुख्य बंडल में छोड़ने में सक्षम होना चाहिए।

हालांकि, यह शायद एक अच्छा अभ्यास नहीं है। यदि आप भविष्य में अपने आवेदन को डेटाबेस लेखन की अनुमति देने के लिए विस्तारित करना चाहते थे, तो आपको फिर से सबकुछ समझना होगा ...

+3

@ विली आवश्यक नहीं है। यदि वह भविष्य में अपना ऐप विस्तारित करना चाहता था, तो वह हमेशा अद्यतन में उपर्युक्त कार्यक्षमता जोड़ सकता था। निर्बाध रूप से काम करना चाहिए। –

संबंधित मुद्दे