I have a function in an iOS app which reads in some data from a database. The function is extremely simple:
-(void) readCategories {
sqlite3 *database;
if([[NSFileManager defaultManager] fileExistsAtPath:databasePath]){
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "SELECT * FROM Categories ORDER BY name COLLATE NOCASE ASC";
sqlite3_stmt *compiledStatement;
int errorCode = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(errorCode == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
[categories addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
}
} else {
NSLog(@"Error reading categories. Code: %d, message: '%s'", errorCode,sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
} else {
NSLog(@"Error opening DB");
}
} else {
NSLog(@"DB does not exist.");
}
}
The problem is that the errorCode is always SQLITE_ERROR which according to the documentation is: “SQL error or missing database”. The message given is: ‘no such table: Categories’
Now, if I look at the database file on my computer, the table is clearly there. I can also run exactly the same query on it and it works correctly.
Does anyone have any ideas on what is going wrong?
Thanks.
So the bug was that it wasn’t copying over the database file. I still have no idea why it wasn’t. I fortunately have the luxury of being able to create the database file programatically. Doing that solved the problem. Not sure why the file wouldn’t copy however.