When I initially create an SQLite database file with pre-inserted datasets for my application, I would have to place this file somewhere in my Xcode project so that it goes to my iPhone application. I guess "ressources" is the right place for that.
What are the basic "steps" for deployment of an SQLite database file in an iPhone application?
I'm currently reading the whole SQLite documentation, although that's not much iPhone related.
I recommend you create a directory such as (application_home)/Library/Database and save files there.
Step 1 − Open Xcode -→ Single View Application -→ Let's name is DBSqlite. Step 2 − Let's develop our UI, Open Main. storyboard and add one text field and two buttons as shown below. Step 3 − Create @IBAction for both the buttons and @IBOutlet for text field and name them btnInsert, btnShowData and name respectively.
There is no "standard place" for a sqlite database. The file's location is specified to the library, and may be in your home directory, in the invoking program's folder, or any other place.
SQLite is available by default on iOS. In fact, if you've used Core Data before, you've already used SQLite. Core Data is just a layer on top of SQLite that provides a more convenient API.
You need to add the SQLite file to your Xcode project first - the most appropriate place is in the resources folder.
Then in your application delegate code file, in the appDidFinishLaunching method, you need to first check if a writable copy of the the SQLite file has already been created - ie: a copy of the SQLite file has been created in the users document folder on the iPhone's file system. If yes, you don't do anything (else you would overwrite it with the default Xcode SQLite copy)
If no, then you copy the SQLite file there - to make it writable.
See the below code example to do this: this has been taken from Apple's SQLite books code sample where this method is called from the application delegates appDidFinishLaunching method.
// 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]); } }
============
Here's the above code in 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)") } } }
If you're just going to be querying for data, you should be able to leave it in the main bundle.
This, however, is probably not a good practice. If you were to extend your application in the future to allow for database writing, you'd have to figure everything out again...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With