Reputation: 2148
I got to a point where my application works perfectly on the simulator but when I load it onto my iPhone the database does not register. I know I have something wrong with my initialization and copying of the db. The database is not saving to the app bundle I guess. I would like direction on what I am doing wrong.
I plan to edit the data in the database as well so would want this taken into consideration.
Here is the code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Use this section to bring in database and populate the array
dbPath = @"/users/jr/Documents/projectReference/reference.db";
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
// Init the subjects Array
categories = [[NSMutableArray alloc] init];
subjects = [[NSMutableArray alloc] init];
quotes = [[NSMutableArray alloc] init];
quoteMap = [[NSMutableArray alloc] init];
[self copyDatabaseIfNeeded];
// Open the database from the users filessytem
NSLog(@"Going into the while loop for categories");
FMResultSet *result_categories = [database executeQuery:@"select distinct category from SUBJECT"];
}
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *newdbPath = [self dbPath];
BOOL success = [fileManager fileExistsAtPath:newdbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"reference.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:newdbPath error:&error];
NSLog(@"Database file copied from bundle to %@", newdbPath);
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
} else {
NSLog(@"Database file found at path %@", newdbPath);
}
}
Upvotes: 0
Views: 664
Reputation: 2210
your static dir @"/users/jr/Documents/projectReference/reference.db" to database is wrong in a iOS context. you should use relative path detection. if you store your DB at bundle and copy it with other resources, just use this one:
NSString *path = [[NSBundle mainBundle] pathForResource:@"reference" ofType:@"db" inDirectory:@""];
if you create DB at runtime and store it in documents, use this one:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *dbPath = [resourcePath stringByAppendingPathComponent:@"reference.db"]
or just read about this here and here.
Upvotes: 1