0

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.

faisal60
  • 87
  • 3
  • 10

2 Answers2

0

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

link 1

and than try to create tabels

Community
  • 1
  • 1
jayraj m.g.
  • 625
  • 5
  • 18
-1
-(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingString:@"/dbfile.sql"];
}

the path is not right

Himanshu A Jadav
  • 2,286
  • 22
  • 34
Xianng
  • 342
  • 3
  • 14
  • You are copy-pasting OPs code as answer. To comment on something in the code, consider posting a comment instead. – Amar Feb 18 '14 at 08:45