当我添加WHERE子句时,为什么我的查询不返回任何结果?

时间:2022-09-22 15:59:16

My Delphi application is connected to SQLite successfully.

我的Delphi应用程序已成功连接到SQLite。

procedure TForm1.Button1Click(Sender: TObject);
begin
  ZQuery1.Close;
  ZQuery1.SQL.Clear;
  ZQuery1.SQL.Text := 'SELECT Name FROM city;';
  ZQuery1.Open;

  while not ZQuery1.EOF do
  begin
    Memo1.Lines.Add(ZQuery1.FieldValues['name']);
    ZQuery1.Next;
  end;
end;

The above code works fine and loads contents of field name from table city. However,

上面的代码工作正常,并从表城加载字段名称的内容。然而,

procedure TForm1.Button1Click(Sender: TObject);
begin
  ZQuery1.Close;
  ZQuery1.SQL.Clear;
  ZQuery1.SQL.Text := 'Select name from city WHERE district = :aField';
  ZQuery1.Params.ParamByName('aField').Value := 'kabol';
  ZQuery1.Open;

  while not ZQuery1.EOF do
  begin
    Memo1.Lines.Add(ZQuery1.FieldValues['name']);
    ZQuery1.Next;
  end;
end;

Surprisingly, when I add a where clause, the query returns nothing! Could anyone suggest what is wrong in my code?

令人惊讶的是,当我添加一个where子句时,查询什么都不返回!任何人都可以建议我的代码有什么问题吗?

Here is an image of the data in my table: 当我添加WHERE子句时,为什么我的查询不返回任何结果?

这是我表中数据的图像:

2 个解决方案

#1


4  

You probably don't have any data that has a district of kabol. The addition of the WHERE clause would then result in no rows being returned, meaning that ZQuery1.Eof is immediately true, and your while not ZQuery1.Eof do loop never gets entered.

您可能没有任何具有kabol区域的数据。然后添加WHERE子句将导致不返回任何行,这意味着ZQuery1.Eof立即为true,而您的while循环从未进入ZQuery1.Eof do循环。

You can check this by changing your first query (the one that works) to something like this:

您可以通过将第一个查询(有效的查询)更改为以下内容来检查:

ZQuery1.SQL.Text := 'SELECT Name, District FROM City';

Then change the output to

然后将输出更改为

Memo1.Lines.Add(ZQuery1.FieldValues['name'] + #9 +
                ZQuery1.FieldValues['district']);

If you don't see at least one line in the memo that contains kabol in the rightmost column, you don't have any rows that match your WHERE criteria. (Note that most databases are case-sensitive, so kabol is not equal to Kabol; the first would match your WHERE, but the second would not.)

如果备注中至少有一行在最右边的列中包含kabol,则表示没有符合WHERE条件的行。 (请注意,大多数数据库都区分大小写,因此kabol不等于Kabol;第一个匹配您的WHERE,但第二个不匹配。)

#2


1  

Your screenshot shows one database row where district is 'Kabol' (uppercase K), but your SQL query is looking for 'kabol' (lowercase k) instead. Assuming the query is comparing strins case-sensitively, that would explain why no row is found. So either fix the case in your query input, or else perform a case-insensitive query instead.

您的屏幕截图显示了一个数据库行,其中分区是'Kabol'(大写K),但您的SQL查询正在寻找'kabol'(小写k)。假设查询以区分大小写的方式比较strins,这可以解释为什么没有找到行。因此,要么在查询输入中修复大小写,要么执行不区分大小写的查询。

#1


4  

You probably don't have any data that has a district of kabol. The addition of the WHERE clause would then result in no rows being returned, meaning that ZQuery1.Eof is immediately true, and your while not ZQuery1.Eof do loop never gets entered.

您可能没有任何具有kabol区域的数据。然后添加WHERE子句将导致不返回任何行,这意味着ZQuery1.Eof立即为true,而您的while循环从未进入ZQuery1.Eof do循环。

You can check this by changing your first query (the one that works) to something like this:

您可以通过将第一个查询(有效的查询)更改为以下内容来检查:

ZQuery1.SQL.Text := 'SELECT Name, District FROM City';

Then change the output to

然后将输出更改为

Memo1.Lines.Add(ZQuery1.FieldValues['name'] + #9 +
                ZQuery1.FieldValues['district']);

If you don't see at least one line in the memo that contains kabol in the rightmost column, you don't have any rows that match your WHERE criteria. (Note that most databases are case-sensitive, so kabol is not equal to Kabol; the first would match your WHERE, but the second would not.)

如果备注中至少有一行在最右边的列中包含kabol,则表示没有符合WHERE条件的行。 (请注意,大多数数据库都区分大小写,因此kabol不等于Kabol;第一个匹配您的WHERE,但第二个不匹配。)

#2


1  

Your screenshot shows one database row where district is 'Kabol' (uppercase K), but your SQL query is looking for 'kabol' (lowercase k) instead. Assuming the query is comparing strins case-sensitively, that would explain why no row is found. So either fix the case in your query input, or else perform a case-insensitive query instead.

您的屏幕截图显示了一个数据库行,其中分区是'Kabol'(大写K),但您的SQL查询正在寻找'kabol'(小写k)。假设查询以区分大小写的方式比较strins,这可以解释为什么没有找到行。因此,要么在查询输入中修复大小写,要么执行不区分大小写的查询。