I am trying to read data from a sqlite database and display it in a table view in an ios app. But I am not able to open the sqlite database for some reason and the app crashes with the message – Failed to open database.
I created the database using the Firefox sqlite manager and copied the file into the Xcode project.
Here’s my code –
-(NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:kFilename];
}
I am trying to open the database using the sqlite3_open_v2 command inside the viewDidLoad
sqlite3 *database;
if (sqlite3_open_v2([[self dataFilePath] UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
sqlite3_close(database); // not sure you need to close if the open failed
NSAssert(0, @"Failed to open database");
}
This is how I open my database. I have not been doing SQLite for a long time and I am not able to give you much help, other than providing this code that I use in my own application. This is from a class that I use in my app.