在使用此代码复制数据库时,我不断收到错误“SQLite Step Failed:尝试编写只读数据库”:
-(void)createEditableCopyOfDatabaseIfNeeded
{
// Testing for existence
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath =
[documentsDirectory stringByAppendingPathComponent:@"Money.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to
// the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Money.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath
toPath:writableDBPath
error:&error];
if(!success)
{
NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
[error localizedDescription]);
}
}
我在AppDelegate中使用上面的代码 而这:
NSString *writableDBPath =
[[NSBundle mainBundle] pathForResource:@"Money"
ofType:@"sqlite"];
在ViewController.m中
我正在使用http://th30z.netsons.org/2008/11/objective-c-sqlite-wrapper/我做错了什么?
这种情况一次又一次地发生......之前工作正常,但问题又开始了。
答案 0 :(得分:7)
问题是你不能在你的包中写任何东西。为了能够更改数据库,您需要将其复制到应用程序的文档或缓存文件夹并使用该副本。