如何在设计时拼接 Delphi FireDAC 查询代码

How to Splice Delphi FireDAC Query Code at Design Time

我收到此语法错误消息:

[FireDAC][Phys][SQLite] ERROR: near "ON": syntax ERROR

关于我的代码:

  qryItems.Active := False;
  qryItems.ResourceOptions.ParamCreate := False;
  qryItems.SQL.Text := 'SELECT category.name, item.name, item.description' +
                       'FROM item ' +
                       'JOIN category ON item.category_id = category.list_id' +
                       'WHERE item.name = :searches OR :searches IS NULL' +
                       'ORDER BY category.name LIMIT 5';
  qryItems.ParamByName('searches').AsString := Format('%%%s%%',[edtSearch.Text]);
  qryItems.Active := True;
  qryItems.SQL.Clear;
  qryItems.ExecSQL;

我在 TFDQuery 的查询编辑器中尝试了 运行 代码,没有任何问题。我不得不对查询进行切片以避免使用此语法“+”违反长文本 — 我希望这仍然是惯例。

看来我的语法没有问题。否则,我会在这里错过一些东西。

SQL 行的结尾如下(注意 JOIN、WHERE 和 ORDER,突出显示以强调):

SELECT category.name, item.name, item.descriptionJOIN category ON item.category_id = category.list_idWHERE item.name = :searches OR :searches IS NULLORDER BY category.name LIMIT 5

您可以像这样用多行设置 SQL:

  qryItems.SQL.Clear;
  qryItems.SQL.AddStrings(TArray<string>.Create(
      'SELECT category.name, item.name, item.description',
      'JOIN category ON item.category_id = category.list_id',
      'WHERE item.name = :searches OR :searches IS NULL',
      'ORDER BY category.name LIMIT 5'));

也许声明一个常量以提高可读性:

const
  cSQL: TArray<string> = [
    'SELECT category.name, item.name, item.description',
    'JOIN category ON item.category_id = category.list_id',
    'WHERE item.name = :searches OR :searches IS NULL',
    'ORDER BY category.name LIMIT 5'];
...
  qryItems.SQL.Clear;
  qryItems.SQL.AddStrings(cSQL);