sqlite3_open returns an error if the database does not already exist. To create the database if it doesn't already exist, use sqlite3_open_v2 with the SQLITE_OPEN_CREATE and SQLITE_OPEN_READWRITE flags (both are required):
rc = sqlite3_open_v2(/* DB Path */, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
Reference
To find /* DB PATH */, you need to base the filename off of the documents directory:
- (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
You can use this convenience method to combine the documents path with the database name:
NSString *documents = [self applicationDocumentsDirectory];
NSString *dbPath = [documents stringByAppendingPathComponent:@"test.db"];
rc = sqlite3_open_v2([dbPath UTF8String], &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);