Sqlite_prepare_v2返回SQLite_ERROR

时间:2012-10-22 08:01:18

标签: ios database sqlite syntax-error

我必须从SQLite数据库中打开一个包含空字段的表,因此,在给出要打开的表的名称和文档文件夹中的数据库地址(好的)后,我设置了查询,然后我使用函数sqlite_prepare_v2来准备数据提取,但返回值1SQLite_ERROR)并给出语法错误。为什么?我写了正确的字段名称。

-(NSMutableArray*)loadDatiFromTable:(NSString*) nameTable {

    NSString *query =[NSString stringWithFormat:@"SELECT id,title, ifnull(IMAGES,''),ifnull(text, ''), ifnull(NUMBER, ''),ifnull(CAP, 0) FROM %@ order by ID", nameTable];  
    sqlite3_stmt *statement;
    int sqlResult = sqlite3_prepare_v2(database, [query UTF8String], -1, &statement,nil);
    NSLog(@"The value is %d", sqlResult); // here sqlResult is 1

    if (sqlResult == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            //......
        }
        sqlite3_finalize(statement);
    } else {
        NSLog(@"Failed from sqlite3_step(statement). Error is:  %s", sqlite3_errmsg(database) );
        // here return: "Failed from sqlite3_step(statement). Error is:  near "NUMBER": syntax error"
       sqlite3_close(database);
   }

   return self.tableArray;
}


-(id) initWithTable:(NSString*)nomeTabella withDbPath: (NSString *)dbPath { 
    self = [super init];
    if (self) {
        if (sqlite3_open([dbPath UTF8String], &database) != SQLITE_OK) {
            NSLog(@"Failed open database");
        } else {
            NSLog(@"OPEN DATABASE");
            [self loadDatiFromTable:nomeTabella ];
        }

        return self;
    }
}

错误消息是:

    "Failed from sqlite3_step(statement). Error is:  near "NUMBER": syntax error"

1 个答案:

答案 0 :(得分:0)

使用"NUMBER"代替NUMBER以确保此单词不会被解释为SQL关键字。