使用 SET 和 WHERE 访问更新记录 table

UPDATE record using SET and WHERE for ACCESS table

我试图在简单的 MS Access table (tblCustomer) 上允许记录更新。要编辑记录,对于 SET 子句,用户从组合框 (cboSetField) 中选择一个字段名称,然后将文本输入相关的文本框 (txtSet)。对于 WHERE 子句,用户从组合框 (cboWhereField) 中选择一个字段,然后将文本输入关联的文本框 (txtWhere)。所以,基本上,我在下面的代码中所拥有的类似于:

更新 tblCustomers SET [cboSetField] = [txtSet] WHERE [cboWhereField] = [txtWhere]

这是实际的 Delphi 代码:

procedure TfrmDeleteCustomer.cmdUpdateClick(Sender: TObject);
var WhereFieldSelection,WhereTextSelection, SetFieldSelection, SetTextSelection :string;
begin

WhereFieldSelection:=cboWhereField.Text;
WhereTextSelection:=txtWhere.Text;
SetFieldSelection:=cboSetField.Text;
SetTextSelection:=txtSet.Text;

adoQuery1.SQL.Clear;
adoQuery1.SQL.Add('UPDATE tblCUSTOMER');
adoQuery1.SQL.Add('SET (:SetFieldSelection) = (:SetTextSelection)');
adoQuery1.SQL.Add('WHERE (:WhereFieldSelection) = (:WhereTextSelection)');
adoQuery1.Parameters.ParamByName('SetFieldSelection').Value:= SetFieldSelection;
adoQuery1.Parameters.ParamByName('SetTextSelection').Value:= SetTextSelection;
adoQuery1.Parameters.ParamByName('WhereFieldSelection').Value:= WhereFieldSelection;
adoQuery1.Parameters.ParamByName('WhereTextSelection').Value:= WhereTextSelection;
adoQuery1.ExecSQL;
adoQuery1.Close;

txtSet.Text:='';
txtWhere.Text:='';
cboSetField.Text:='';
cboWhereField.Text:='';
adoQuery1.SQL.Clear;
adoQuery1.SQL.Add('SELECT * FROM tblCUSTOMER');
adoQuery1.Open;
end;

现在,当我 运行 程序时,我收到以下错误消息:

Exception Class Raised: Syntax Error in UPDATE Statement

我做错了什么?

使用:Delphi7、ADO 连接,MS ACCESS 2003。

通常在大多数数据库 API 中,例如 ADO(或 PHP 的 PDO 或 Python 的游标)SQL 查询中的参数涉及传递 在查询中,不将 字段名称 传递给 table.

动态考虑 formatting 然后使用参数化值的 SQL 字符串:

 adoQuery1.SQL.Add(format('UPDATE tblCUSTOMER SET %S = :SetTextSelection', [SetFieldSelection]));
 adoQuery1.SQL.Add(format('WHERE %S = :WhereTextSelection', [WhereFiedSelection])); 
 adoQuery1.Parameters.ParamByName('SetTextSelection').Value:= SetTextSelection; 
 adoQuery1.Parameters.ParamByName('WhereTextSelection').Value:= WhereTextSelection;
 adoQuery1.ExecSQL;