English 中文(简体)
使用字符串参数 xcode 的 sqlite 查詢
原标题:sqlite query with string parameter xcode
  • 时间:2012-05-25 18:42:38
  •  标签:
  • xcode
  • sqlite

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

时 时

问题回答

看来您需要在 周围单引号。 如果不是的话, 请试着追踪错误消息的内容 。





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...