आपको पहले अपने एक्सकोड प्रोजेक्ट में 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)")
}
}
}
क्या आप कोड प्राप्त कर सकते हैं, जहां से आप कोड प्राप्त कर सकते हैं? धन्यवाद। – itsaboutcode
यदि आप इसे लिखना चाहते हैं तो डेटाबेस कहां होना चाहिए? मुझे पता है कि अगर यह रिसोर्स समूह (मुख्य बंडल) में है तो यह केवल पढ़ने के लिए है, तो दोनों लिखने और पढ़ने की अनुमति देने का सही तरीका कौन सा है? धन्यवाद! –
मुझे पता है कि यह थोड़ा देर हो चुकी है, लेकिन मैं यह जानने के लिए कैसे जाउंगा कि इसे मुख्य बंडल से अपडेट करने की आवश्यकता है या नहीं? मैंने फ़ाइल विशेषताओं की जांच करने का प्रयास किया, लेकिन जब भी मैं एक नया संस्करण संकलित करता हूं तो ऐप में फ़ाइल की संशोधन तिथि बदल जाती है। – Flipper