【问题标题】:HOW TO Using ADOQuery sum in Delphi如何在 Delphi 中使用 ADOQuery sum
【发布时间】:2018-09-30 21:53:04
【问题描述】:

我有这个 SQL 查询:

如您所见,它运行良好。除非我在 Delphi 中使用它。

我使用 TDBLookupComboBox、TEdit 和 TButton。我的想法是,每当我点击 TButton 时,EXE 就会使用 DBLookupComboBox 中的信息检查 SQL 中的表,并将结果放入 TEdit。

这是我的 Delphi 代码:

【问题讨论】:

标签: sql-server delphi-7 delphi-xe2 tadoquery


【解决方案1】:

在您的 Delphi 代码中,您使用双引号:"

您的字符串需要用单引号括起来:'

要在 Delphi 字符串中嵌入单引号,需要使用两个单引号:''

S := 'hey"there';        // hey"there

S := 'hey''there';       // hey'there

S := 'specification='''; // specification='

S := '''';               // '

【讨论】:

  • 像这样:ADOQuery1.SQL.Add('where specification =;"'+dblookupcombobox1.text+'"'); ???
  • 使用参数化查询不仅可以使这项工作更好,即使用户在 DBLookupCombo 中输入单引号,它也可以防止 sql 注入。
【解决方案2】:

试试这个代码。

Chr(39) in delphi = " ' " sign. 

希望对你有所帮助。

Begin
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('Select Sum(Cast(Total As Float)) As tot From machine Where Specification='+Chr(39)+dbLookupComboBox1.Text+Chr(39));
AdoQuery1.Open;
End;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多