How do you create a writable copy of a sqlite database that is included in your iPhone Xcode project

Posted by Iggy on Stack Overflow See other posts from Stack Overflow or by Iggy
Published on 2010-03-17T20:39:52Z Indexed on 2010/03/17 20:41 UTC
Read the original article Hit count: 211

Filed under:
|
|

copy it over to your documents directory when the app starts. Assuming that your sqlite db is in your project, you can use the below code to copy your database to your documents directory. We are also assuming that your database is called mydb.sqlite.

    //copy the database to documents
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
if(![fileManager fileExistsAtPath:path])
{
    NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/mydb.sqlite"]];
    [data writeToFile:path atomically:YES];
}

Anyone know of a better way to do this. This seems to work ...

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk