0

我正在尝试查询我的 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;

}

4

1 回答 1

0

看起来您需要在%@. 如果不是这样,请尝试追踪错误消息是什么。

于 2012-05-25T18:44:33.817 回答