tags:

views:

100

answers:

1

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

+1  A: 

That looks valid to me. You can also just copy the file with this NSFileManager method instead:

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;

I think using this method instead might be a bit more readable, but YMMV.

Shaggy Frog
This is much better because it doesn't copy the whole database into memory first!
Kendall Helmstetter Gelner
You're right. I assumed it was probably small because all of my SQLite DBs are small. But in this case it didn't hurt me. :)
Shaggy Frog