sqlite3_open
如果数据库尚不存在,则返回错误。如果数据库尚不存在,要创建数据库,请使用sqlite3_open_v2
andSQLITE_OPEN_CREATE
标志SQLITE_OPEN_READWRITE
(两者都是必需的):
rc = sqlite3_open_v2(/* DB Path */, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
参考
要查找/* DB PATH */
,您需要基于文档目录的文件名:
- (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
您可以使用这种方便的方法将文档路径与数据库名称结合起来:
NSString *documents = [self applicationDocumentsDirectory];
NSString *dbPath = [documents stringByAppendingPathComponent:@"test.db"];
rc = sqlite3_open_v2([dbPath UTF8String], &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);