Where would you place your SQLite database file in an iPhone app? Where would you place your SQLite database file in an iPhone app? sqlite sqlite

Where would you place your SQLite database file in an iPhone app?


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...