【问题标题】:How to run SQLite Query even if Input is Blank即使输入为空,如何运行 SQLite 查询
【发布时间】:2012-05-15 17:17:18
【问题描述】:

我正在做一个 iPad 项目,UI 中有 3 个表格。如果我在每个表中选择行。 Sqlite 查询将从表中获取值并运行查询。请在下面找到我的查询。

NSString * str3=tableFou.string4;
    NSString * str4=tableFiv.string5;
    NSString * str2=tablethr.string3;

    sqlite3 *database;
    favorite=[[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
        NSString *sql=[NSString stringWithFormat:@"Create view prizeview as SELECT country.name,item_country.id,text.text,substr(text,1,2) FROM country,item_country,prize,item_prize,gender,item_gender,text WHERE country.name = '%@' AND text.item = item_country.item AND country.id = item_country.id AND prize.name = '%@' and text.item=item_prize.item and prize.id = item_prize.id and gender.name = '%@' and text.item=item_gender.item and gender.id = item_gender.id ",str3,str4,str2];
        const char *sqlStatement = [sql UTF8String];;

问题是,如果我只选择所有表,则查询正在运行。但如果我选择一个表,我想要结果。我该怎么做?

【问题讨论】:

  • 为此,您使用 if 条件传递。尝试使用 and 条件,并且您使用 str4 和 str 5 和 str4,str5 如果任何条件为真查询运行
  • 谢谢,但如果我有三张桌子..那么
  • 你只有一个 if 条件,所以如果你有三个或更多,那么在 if 条件中使用那么多和条件。它只增加两行代码。
  • 因为如果你一个接一个地使用它对你来说又大又复杂。

标签: iphone ipad sqlite


【解决方案1】:

尝试使用 if 条件使用 with and 条件并将所有 str 传递到 if 条件中。

像这样使用你的 if 条件。

if(([propertyTypeLabel.text length]==0) || ([cityLabel.text length]==0 ) ||( [locationLabel.text length]==0 )|| ([priceLabel.text length] ==0)||([postedLabel.text length]==0))

在这种情况下,任何文本都是零条件,如果表格单元格的蚂蚁点击您的查询运行,您也可以使用。

【讨论】:

  • 我听不懂你想说什么!!!..你能在这里写一个示例代码吗..这对我有更好的帮助
  • 对不起,您尝试使用 || 的 && 即时我认为这对你会有帮助。
  • 我不明白逻辑如果label.text为0,那么查询中的值也会变为零,那么查询将如何运行???
  • 嘿,我只是向您展示了您如何使用条件的示例,您不遵循条件的内容,使用您自己的表格 str 条件等待我为您修改。
  • 请你告诉我那个 tableFou.string4 是什么。
【解决方案2】:

您无法搜索空字段,除非该字段实际上是空的。前任如果你搜索

select * from mytable where username = "";

你不会找到它,但如果你可以接受该字段为空或任何结果,你必须使用 like 而不是 =

select * from mytable where username like "%";

或者更好的是,您可以像这样创建查询,假设您要搜索三个字段,用户名、名字和姓氏。下面的代码需要更多的 if 和 buts,但您应该能够完成这项工作,并且只有在满足要求时才将参数添加到 where。

NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:@"select * from table where "];
if (username.text.lenghth >1) [query appendString:[NSString stringWithFormat:@"username = '%@'"]];
if (firstname.text.length >1) [query appendString:[NSString stringWithFormat:@"&& firstname = '%@'"]];
if (lastname.text.length >1) [query appendString:[NSString stringWithFormat:@"&& lastname = '%@'"]];

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 2013-01-07
    • 2011-08-02
    • 2021-08-16
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多