Created
July 8, 2011 07:31
-
-
Save popcornylu/1071312 to your computer and use it in GitHub Desktop.
[ios] Copy sqlite db from main bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; | |
NSString* dbPath = [docPath stringByAppendingPathComponent:@"mydatabase.db"]; | |
NSFileManager *fm = [NSFileManager defaultManager]; | |
// Check if the database is existed. | |
if(![fm fileExistsAtPath:dbPath]) | |
{ | |
// If database is not existed, copy from the database template in the bundle | |
NSString* dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"mydatabase" ofType:@"db"]; | |
NSError* error = nil; | |
[fm copyItemAtPath:dbTemplatePath toPath:dbPath error:&error]; | |
if(error){ | |
NSLog(@"can't copy db template."); | |
assert(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment