Reputation: 93
I have a problem with my Objective C code and an SQLite implementation. The error line is NSAsset
in the if clause, thats comment out. Here is the code:
-(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 create");
}
}
-(NSString *) filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}
-(void)openDB{
if (sqlite3_open([[self filePath] UTF8String], &db) !=SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Database failed to open");
}else{
NSLog(@"Database open");
}
}
And the error message:
*** Assertion failure in -[MainViewController createTable:withField1:withField2:withField3:withField4:], /Users/Modius/X-Code 4/BPApp/BPApp/MainViewController.m:32
2013-01-18 11:38:07.772 BPApp[58857:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create table'
*** First throw call stack:
(0x2090012 0x119de7e 0x208fe78 0xc33f35 0x20a0 0x243a 0x1c6817 0x1c6882 0x115a25 0x115dbf 0x115f55 0x11ef67 0xe2fcc 0xe3fab 0xf5315 0xf624b 0xe7cf8 0x1febdf9 0x2013f3f 0x201396f 0x2036734 0x2035f44 0x2035e1b 0xe37da 0xe565c 0x1c3d 0x1b65 0x1)
libc++abi.dylib: terminate called throwing an exception
UPDATE: Here the complete Log Output:
2013-01-18 12:18:14.456 BPApp[59854:c07] Database open
2013-01-18 12:18:14.458 BPApp[59854:c07] *** Assertion failure in -[MainViewController createTable:withField1:withField2:withField3:withField4:], /Users/Modius/X-Code 4/BPApp/BPApp/MainViewController.m:31
2013-01-18 12:18:14.460 BPApp[59854:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create table'
*** First throw call stack:
(0x2090012 0x119de7e 0x208fe78 0xc33f35 0x20a0 0x243a 0x1c6817 0x1c6882 0x115a25 0x115dbf 0x115f55 0x11ef67 0xe2fcc 0xe3fab 0xf5315 0xf624b 0xe7cf8 0x1febdf9 0x1febad0 0x2005bf5 0x2005962 0x2036bb6 0x2035f44 0x2035e1b 0xe37da 0xe565c 0x1c3d 0x1b65 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Upvotes: 0
Views: 166
Reputation: 9935
Try to format it like this:
@"CREATE TABLE IF NOT EXIST '%@' ( '%@' TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);"
Upvotes: 0
Reputation: 180300
NSString *sql= [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXIST '%@' ( '%@ "
"TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);",
tableName, field1, field2, field3, field4];
Your are missing a single quote after the first field name.
(Please note that in SQL, table/field names should be quoted with double quotes; SQLite accepts single quotes only for compatibility with MySQL.)
Upvotes: 1