我正在尝试查询我的sqlite数据库,其中列等于传递给函数的值。
我的查询看起来像这样
NSString *query=[NSString stringWithFormat:@"SELECT rowid, name FROM Coops WHERE category=%@",chosenCategory];
此查询只返回任何内容,但如果我更改它,则它看起来像这样
query=@"SELECT rowid, name FROM Coops WHERE category='the category I am passing';
它运作得很好。我的问题是如何将字符串参数传递给xcode中的sqlite查询。
我的整个方法就是这个
- (NSArray *)coopsInCategory:(NSString *)selectedCategory {
sqlite3_stmt *statement;
NSString *query=[NSString stringWithFormat:@"SELECT rowid, name, hCity, hours FROM Coops WHERE category=%@",chosenCategory];
/*if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, NULL)!=SQLITE_OK) {
NSAssert1(0, @"Error preparing statment",sqlite3_errmsg(_database));
}*/
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)== SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_ROW)
{
int uniqueId=sqlite3_column_int(statement, 0);
char *nameChar=(char *) sqlite3_column_text(statement, 1);
char *hCityChar=(char *) sqlite3_column_text(statement, 2);
char *hoursChar=(char *) sqlite3_column_text(statement, 3);
NSString *name=[[NSString alloc] initWithUTF8String:nameChar];
NSString *hCity=[[NSString alloc] initWithUTF8String:hCityChar];
NSString *hours=[[NSString alloc]initWithUTF8String:hoursChar];
informationObject *info=[[informationObject alloc]coopDetails:uniqueId name:name hCity:hCity hours:hours];
[retval addObject:info];
[name release];
[hCity release];
[hours release];
}
sqlite3_finalize(statement);
}
return retval;
}
答案 0 :(得分:0)
看起来你需要%@
周围的单引号。如果不是这样,请尝试追踪错误消息。