iOS错误:由于未捕获的异常'NSInternalInconsistencyException'而终止应用

时间:2012-08-29 14:01:47

标签: ios ipad sqlite exception

我是新开发者,我收到此错误。 我厌倦了通过两次添加libsqlite3.0.dylib并重新启动x代码来修复它。 即使我添加libsqlite3.dylib的其他库,但didint工作。 错误: 由于未捕获的异常'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"];
}

1 个答案:

答案 0 :(得分: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;
}