【发布时间】:2012-12-16 06:53:04
【问题描述】:
我无法将我的 sqlite 数据库复制到用户文档文件夹,数据库总是 0 字节但应该有 322 kb。
我还检查了数据库是否包含在目标成员中。
问题是我无法正确复制数据库。
这是我的代码:
-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"wine.sqlite"];
// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{
// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
// Get the path to the database in the application package
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"wine" ofType:@"sqlite"];
//NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"wine.sqlite"];
// Copy the database from the package to the users filesystem
[[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
NSLog(@"Database created");
}
}
}
【问题讨论】:
-
这个问题的题目是错误的。你想从 INSIDE 包复制到文件夹,而不是从项目目录 - 编辑它
标签: iphone ios database sqlite ios-simulator