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.