tags:

views:

4733

answers:

5

When I create initially an SQLite database file with pre-inserted datasets for my app, I would have to place this file somewhere in my Xcode project so that it goes to my iphone app. I guess "ressources" is the right place for that.

What are the basic "steps" for deployment of an SQLite database file in an iPhone app?

  • creating the db manually
  • adding the db file to the project (where?)

I'm currently reading the whole SQLite documentation, although that's not much iPhone related. I know you know it! thanks guys ;)

+10  A: 

hey u need to add the sqlite file to your xcode project first - most appropriate place is in the resources folder

then in your app 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 dont 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 below code example to do this: this has been taken from apple's sqlite books code sample where this method is called from the app 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]);
    }
}
Raj
@Raj, can you please the page from where you get the code? Thanks.
itsaboutcode
+1  A: 

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 app in the future to allow for db writing, you'd have to figure everything out again...

Willi Ballenthin
A: 

@ Willi

Not necessarily true. If he were to extend his app in the future, he could always just add the above functionality in an update. Should work seamlessly.

Arlen
A: 

I would also like to know how to write data to a sqlite database!

Everything I have tried is either depricated or results in a SIGBRT error

Thanks in Advance

Hbashir

Hbashir Naij
A: 

U just keep ur sqlite database in ur xcode project folder

Chinju