2013-01-18 2 views
0

Objective C 코드와 SQLite 구현에 문제가 있습니다. 오류 줄은 if 절에 NSAsset이고 주석은 out입니다.sqlite 문제

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

그리고 오류 메시지 :

*** 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 : 다음 코드는 여기에 전체 로그 출력 :

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) 
+0

NSLog 출력을 게시 할 수 있습니까? – Raptor

+0

db 닫기를 확인하기 전에 NSLog (@ "Error % s", sqlite3_errmsg (yourSqlite3Obj)); –

+0

코드에 sqlite3Obj가 없습니다. sry은 sql3lite가있는 첫 번째 iOS 앱입니다. –

답변

0
NSString *sql= [NSString stringWithFormat: 
       @"CREATE TABLE IF NOT EXIST '%@' ('%@ " 
       "TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);", 
       tableName, field1, field2, field3, field4]; 

귀하가 후 작은 따옴표를 누락 첫 번째 필드 이름.

(. SQL에 있다는 점 유의 하시길 바랍니다 테이블/필드 이름을 따옴표로 인용되어야한다 SQLite는 단지 MySQL의와의 호환성을 위해 작은 따옴표를 받아)

+0

확인을 추가했지만 오류가 여전히 있습니다. –

0

시도는 다음과 같이 포맷하기 : @"CREATE TABLE IF NOT EXIST '%@' ('%@' TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);"

+0

확인했는데 오류 메시지가 같습니다. –

0

EXIST 대신 EXIST를 읽도록 SQL 문을 수정하십시오.