0

我是一名新开发人员,我收到此错误。我厌倦了通过两次添加 libsqlite3.0.dylib 并重新启动 x-code 来修复它。即使我添加了 libsqlite3.dylib 的另一个库,但没有工作。错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法创建带有消息的可写数据库文件

-(void)copyDatabaseIfNeeded
{
    NSFileManager * filemanger = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbpath= [self getDbPath];
    BOOL success = [filemanger fileExistsAtPath:dbpath];

    if(!success)
    {
        NSString *defaultDBpath= [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"myDatabase.sqlite"];
        success = [filemanger copyItemAtPath:defaultDBpath toPath:dbpath error:&error];
        if(!success)
        {
            NSAssert1(0, @"Failed to create writeable database file with message '@' .", [error localizedDescription]);
        }
    }

}
-(NSString *)getDbPath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [path objectAtIndex:0];
    return[documentsDir stringByAppendingPathComponent:@"myDatabase.sqlite"];
}
4

1 回答 1

0

来吧,这个函数打开一个数据库,如果它不存在于文档中,它会从bundle中复制它。您需要从头开始创建一个吗?

-(BOOL) openDB: (NSString *) dbName{
    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    dbPath = [dbPath stringByAppendingPathComponent:dbName];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dbPath]) {

        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:dbName];   
        [fileManager copyItemAtPath:sourcePath toPath:dbPath error:nil];
    }

    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))           
    {               
       NSLog(@"An error has occured opening Sqlite");  
       return FALSE;
    }

    return TRUE;
}
于 2012-08-29T14:29:46.013 回答