1

我必须从包含一些空字段的 SQLite 数据库中打开一个表,因此,在给出要打开的表的名称和文档文件夹中的数据库地址(好吧)之后,我设置了查询,然后我使用了函数sqlite_prepare_v2准备提取数据,但返回值1( SQLite_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"
4

1 回答 1

0

使用"NUMBER"而不是NUMBER确保该词不被解释为 SQL 关键字。

于 2012-10-22T08:17:30.947 回答