我必须从具有一些空字段的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/