ios - Sqlite_prepare_v2返回SQLite_ERROR

标签 ios database sqlite syntax-error

我必须从具有一些空字段的SQLite数据库中打开一个表,因此,在给出要打开的表的名称和documents文件夹中的数据库地址(好的)之后,我设置了查询,然后使用了函数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"

最佳答案

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

关于ios - Sqlite_prepare_v2返回SQLite_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13007202/

相关文章:

iphone - 自定义委托(delegate)仅在从 UIButton 选择器 ARC 保留问题直接调用时才有效

objective-c - 为什么释放我的阵列会使我的应用程序崩溃?

database - oAuth 2.0 数据库结构

php - SQLite通过php将数据持久保存到MySQL服务器iOS

SQlite:unix 时间戳的列格式;整数类型

ruby-on-rails - Rails Model.create SQLite3::SQLException

ios - 在检测到隐私设置更改时让应用程序自行重启

ios - 具有多个字段的 JSON 字符串

python - 在 Python 中通过 SSH 隧道连接到 PostgreSQL 数据库

sql - 如何获取SQL Server中sql表中没有一列的所有列名