I am really new in ios development. What I try to do now is open an existing sqlite database and select data from there. I debug my source and see that I open the database success (I think I success since the *database pointer is not nil). but when I use sqlite3_prepare_v2() to initialize the select query, I always receive the error: “No such table People”. I have checked at the path:
~/Library/Application Support/iphone simulator/6.0/Application//.
The database was copied successful, I can open it a see the data there.
Here is my code to copy the database and open it:
- (NSString*) getDatabasePath{
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"data.sqlite"];
}
- (void)copyDatabaseToDocument {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDatabasePath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (sqlite3*)openDatabaseConnection {
sqlite3 *database;
NSString * path = [self getDatabasePath];
if (sqlite3_open([path UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
return database;
}
And here is my code to select data. The error occurs at line: sqlite3_prepare_v2(database, query, -1, &selectStatement, NULL)
- (People*) getPeople{
sqlite3 *database = [[[DBConnector alloc] init] openDatabaseConnection];
if(database == nil)
return nil;
sqlite3_stmt *selectStatement;
NSString *rawquery = @"select * from people";
const char *query = [rawquery UTF8String];
NSMutableArray* result = [[NSMutableArray alloc] init];
if (sqlite3_prepare_v2(database, query, -1, &selectStatement, NULL) == SQLITE_OK) {
while (sqlite3_step(selectStatement) == SQLITE_ROW) {
//Parse the data by calling a private method:
People *people = [self parsePeopleWithStatement:selectStatement];
[result addObject:people];
}
}else{
NSAssert1(0, @"Error: '%s'.", sqlite3_errmsg(database));
}
sqlite3_finalize(selectStatement);
return result;
}
Please tell me if you know what the mistake I have.
Thanks.
I have resolved the problem by myself. When I debug the app, I see that It didn’t call
applicationDidFinishLaunchingmethod, It callsapplicationDidFinishLaunchingWithOptionsmethod. I just place the code to call thecopyDatabaseToDocumentat theapplicationDidFinishLaunchingWithOptionsmethod and It works.Here is the code of my Delegate class:
By the way, I think this is not a real answer sync I don’t know why my App didn’t start at the applicationDidFinishLaunching(). If you know, please give me a description.
Thanks.