我正试图查询我的 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 *)chosenCategory {
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;
时 时