Objective c SQLite步骤失败:尝试使用包装器写入只读数据库
使用此代码复制数据库时,我不断收到错误“SQLite步骤失败:尝试写入只读数据库”:Objective c SQLite步骤失败:尝试使用包装器写入只读数据库,objective-c,iphone,sqlite,iphone-sdk-3.0,wrapper,Objective C,Iphone,Sqlite,Iphone Sdk 3.0,Wrapper,使用此代码复制数据库时,我不断收到错误“SQLite步骤失败:尝试写入只读数据库”: -(void)createEditableCopyOfDatabaseIfNeeded { // Testing for existence BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearc
-(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中
我在使用我做错了什么
这是一次又一次地发生。。。它之前工作正常,但问题再次出现。问题是您无法向捆绑包写入任何内容。要更改数据库,您需要将其复制到应用程序的“文档”或“缓存”文件夹中,并使用该副本。NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);NSString*documentsDirectory=[paths objectAtIndex:0];NSString*writableDBPath=[DocumentsDirectoryStringByAppendingPathComponent:@“Money.sqlite”];然后当我使用上面的代码。。上面说找不到桌子!工作。。在我再次复制数据库之后