sqlite3表创建错误'NSInternalInconsistencyException'

时间:2014-02-18 06:33:18

标签: ios objective-c sqlite xcode5.0.1

我在使用sqlite3创建表时遇到问题,我在代码中创建了数据库,但是它出现了错误:'NSInternalInconsistencyException',原因:'无法创建表'

我试图查看代码来查找错误,但我找不到任何错误。 这是一些代码。

-(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingString:@"dbfile.sql"];
}


-(void)openDB
{
    if (sqlite3_open([[self filePath]UTF8String], &db) != SQLITE_OK)
    {
        sqlite3_close(db);
        NSAssert(0,@"Database Failed to Open");
    }else{
        NSLog(@"Database Opened !");
    }
}

-(void)createTable:(NSString *)tableName
        withField1:(NSString *)field1
        withField2:(NSString *)field2
        withField3:(NSString *)field3
        withField4:(NSString *)field4
{
    char *err;
    NSString *sql = [NSString stringWithFormat:
                     @"CREATE TABLE IF NOT EXIST '%@' ('%@'TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);",tableName,field1,field2,field3,field4];
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
    {
        sqlite3_close(db);
        NSAssert(0,@"Could not create table");
    }else{
        NSLog(@"Table Created");
    }
}

我导入sqlite3.h但每次运行模拟器时它都会崩溃并给我错误。

请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:0)

再次在您的xcode中复制数据库并检查以下链接,以便将数据库复制到iOS:

link 1

并尝试创建表格

答案 1 :(得分:-1)

-(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingString:@"/dbfile.sql"];
}

路径不对