Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 实现sqlite3时出错_Objective C_Ios_Xcode - Fatal编程技术网

Objective c 实现sqlite3时出错

Objective c 实现sqlite3时出错,objective-c,ios,xcode,Objective C,Ios,Xcode,我正在尝试链接sqlite3、创建表和插入数据,但出现以下错误: 'NSInternalInconsistencyException', reason: 'Error creating table: near "4": syntax error' *** First throw call stack:(0x15ad022 0x173ecd6 0x1555a48 0xa852cb 0x2bea 0x273a 0x1b5a1e 0x114401 0x114670 0x114836 0x11b72a

我正在尝试链接sqlite3、创建表和插入数据,但出现以下错误:

'NSInternalInconsistencyException', reason: 'Error creating table: near "4": syntax error' *** First throw call stack:(0x15ad022 0x173ecd6 0x1555a48 0xa852cb 0x2bea 0x273a 0x1b5a1e 0x114401 0x114670 0x114836 0x11b72a 0x2415 0xec386 0xed274 0xfc183 0xfcc38 0xf0634 0x1497ef5 0x1581195 0x14e5ff2 0x14e48da 0x14e3d84 0x14e3c9b 0xecc65 0xee626 0x20d2 0x2045 0x1)
守则:

- (void) openDBcreateTable {
if (sqlite3_open([[self filePath]UTF8String], &db) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0, @"Failed to open database");
}
//create table named QUESTIONBANK
NSString *createSQL = @"CREATE TABLE IF NOT EXISTS QUESTIONBANK(ROW INTEGER PRIMARY KEY, QUESTION TEXT, ANSWER TEXT, CHOICE1 TEXT, CHOICE2 TEXT, CHOICE3 TEXT, CHOICE 4 TEXT, USERANSWER TEXT);";

char *errorMsg1;

if (sqlite3_exec(db, [createSQL UTF8String], NULL, NULL, &errorMsg1) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0,@"Error creating table: %s", errorMsg1);
}

for (int k = 1; k < 11; k++) {
    NSMutableArray *questionBlocks = [self pullDataFromSomeWhere];
    NSString *question = [NSString stringWithFormat:[questionBlocks objectAtIndex:0]];
    NSString *answer = [NSString stringWithFormat:[questionBlocks objectAtIndex:1]];
    NSString *choice1 = [NSString stringWithFormat:[questionBlocks objectAtIndex:2]];
    NSString *choice2 = [NSString stringWithFormat:[questionBlocks objectAtIndex:3]];
    NSString *choice3 = [NSString stringWithFormat:[questionBlocks objectAtIndex:4]];
    NSString *choice4 = [NSString stringWithFormat:[questionBlocks objectAtIndex:5]];

    char *update = "INSERT OR REPLACE INTO QUESTIONBANK(ROW,QUESTION,ANSWER,CHOICE1,CHOICE2,CHOICE3,CHOICE4)VALUES(?,?,?,?,?,?,?);";


    sqlite3_stmt *stmt;

    if (sqlite3_prepare_v2(db, update, -1, &stmt, nil) == SQLITE_OK) {
        sqlite3_bind_int(stmt, 1, k);
        sqlite3_bind_text(stmt, 2, [question UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 3, [answer UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 4, [choice1 UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 5, [choice2 UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 6, [choice3 UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 7, [choice4 UTF8String], -1, NULL);

    }
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        NSAssert(0,@"Error updating table");
    }
    sqlite3_finalize(stmt);
}
sqlite3_close(db);}
-(void)openDBcreateTable{
如果(sqlite3_打开([[self filePath]UTF8String],&db)!=SQLITE_正常){
sqlite3_关闭(db);
NSAssert(0,@“无法打开数据库”);
}
//创建名为QUESTIONBANK的表
NSString*createSQL=@“如果不存在问题库,则创建表(行整数主键、问题文本、答案文本、选项1文本、选项2文本、选项3文本、选项4文本、用户答案文本);”;
char*errorMsg1;
if(sqlite3_exec(db[createSQL UTF8String],NULL,NULL和errorMsg1)!=SQLITE_OK){
sqlite3_关闭(db);
NSAssert(0,@“创建表时出错:%s”,错误msg1);
}
对于(int k=1;k<11;k++){
NSMutableArray*questionBlocks=[self-PullDataFrom某处];
NSString*question=[NSString stringWithFormat:[questionBlocks对象索引:0]];
NSString*answer=[NSString stringWithFormat:[questionBlocks对象索引:1]];
NSString*choice1=[NSString stringWithFormat:[questionBlocks objectAtIndex:2]];
NSString*choice2=[NSString stringWithFormat:[questionBlocks objectAtIndex:3]];
NSString*choice3=[NSString stringWithFormat:[questionBlocks objectAtIndex:4]];
NSString*choice4=[NSString stringWithFormat:[questionBlocks objectAtIndex:5]];
char*update=“在问题库(行、问题、答案、选项1、选项2、选项3、选项4)中插入或替换值(?,,,,,,,,,,?);”;
sqlite3_stmt*stmt;
if(sqlite3\u prepare\u v2(db,update,-1,&stmt,nil)=SQLITE\u OK){
sqlite3_bind_int(stmt,1,k);
sqlite3\u bind\u text(stmt,2,[question UTF8String],-1,NULL);
sqlite3_bind_text(stmt,3,[answer UTF8String],-1,NULL);
sqlite3_bind_text(stmt,4,[choice1 UTF8String],-1,NULL);
sqlite3\u绑定\u文本(stmt,5,[choice2 UTF8String],-1,NULL);
sqlite3_绑定_文本(stmt,6,[choice3 UTF8String],-1,NULL);
sqlite3\u绑定\u文本(stmt,7,[choice4 UTF8String],-1,NULL);
}
如果(sqlite3_步骤(stmt)!=SQLITE_完成){
NSAssert(0,@“错误更新表”);
}
sqlite3_最终确定(stmt);
}
sqlite3_close(db);}

由于我还是sqlite 3的新手,我也非常确信还有其他错误。在这里,我是否可以在sqlite3中使用任何“VARCHAR”、“BLOB”等(如MySQL)?

删除create table语句中虚假的“4”:

。。。选项4文本…

应该是:

。。。选择文本…


错误信息是不言自明的。

关于您的数据类型问题,我认为这些信息清楚地说明了这一点。总之,SQLite的数据类型有限,并且它不强制执行数据列的类型(因此您可以在任何列中存储任何数据类型)。没有显式的VARCHAR数据类型。

多么糟糕的事。。。英雄联盟我需要睡一会儿。。剩下的看起来还好吧?请批评我采取的方法,如果有的话。。。Thanx又来了…@TeamStar其余的我看还行。