我正在尝试这样做:
sqlite3 *db;
self.dbPath = "tasks.sqlite";
int result = sqlite3_open(self.dbPath, &db);
此后的代码是抛出错误。所以我怀疑它不起作用。我应该把什么作为路径传递?
答案 0 :(得分:1)
NSString *strDatabaseName;
NSString *strDatabasePath;
-(void)getDatabase
{
strDatabaseName = @"questionsdb.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
strDatabasePath = [documentsDir stringByAppendingPathComponent:strDatabaseName];
}
-(BOOL) checkAndCreateDatabase {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:strDatabasePath];
if(success) {
return TRUE;
}
else {
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:strDatabaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:strDatabasePath error:nil];
}
return FALSE;
}
然后从任何方法
sqlite3 *database;
if(sqlite3_open([strDatabasePath UTF8String], &database) == SQLITE_OK)
{
//doSomething
}
我希望它也适合你。 谢谢。