error in sqlite3 table creation 'NSInternalInconsistencyException'

188 Views Asked by At

I'm having a problem in creating table with sqlite3 , I created the database in the code but it gives an error : 'NSInternalInconsistencyException', reason: 'Could not create table'

I tried to review the code to find bugs but I couldn't find any. here is some of the code.

-(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");
    }
}

and I import sqlite3.h but every time I run the simulator it crashes and gave me that error.

please help me,Thank you.

2

There are 2 best solutions below

0
jayraj m.g. On

copy db again in your xcode and check below link for copy database to iOS :

link 1

and than try to create tabels

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

the path is not right