mysql - 如何在sql参数中使用WHERE ID

标签 mysql sql parameters sqlparameter

我有以下内容:

somequery.SQL.Add('UPDATE `someDBname`.`someTABLEname` SET

`client`='''+someForm.Edit1.Text+''', 
`phone`='''+someForm.Edit2.Text+''', 
`email`='''+someForm.Edit3.Text+''', 
`details`='''+someForm.Edit4.Text+''', 
`specials`='''+someForm.Edit5.Text+''', 
`price`='''+someForm.Edit6.Text+''', 
`address`='''+someForm.Edit7.Text+''',
`deadline`='''+someForm.DateTimePicker1.DateTime+''',
`status`='''+someForm.Edit9.Text+''' 

WHERE `id`=''' + inttostr(someDataSetid.Value) + ''';');

我想改用parameters,像这样:

someQuery.SQL.Clear;
someQuery.SQL.Add( 'UPDATE `someDBname`.`someTABLEname` ( client, phone, email, details, specials, price, address, deadline, status ) values ( :client, :phone, :email, :details, :specials, :price, :address, :deadline, :status ) ' ) ;
someQuery.Params.ParamByName( 'client' ).AsString := someForm.Edit1.Text ;
someQuery.Params.ParamByName( 'telefon' ).AsString := someForm.Edit2.Text ;
someQuery.Params.ParamByName( 'email' ).AsString := someForm.Edit3.Text ;
someQuery.Params.ParamByName( 'detalii' ).AsString := someForm.Edit4.Text ;
someQuery.Params.ParamByName( 'mentiuni' ).AsString := someForm.Edit5.Text ;
someQuery.Params.ParamByName( 'pret' ).AsString := someForm.Edit6.Text ;
someQuery.Params.ParamByName( 'livrare' ).AsString := someForm.Edit7.Text ;
someQuery.Params.ParamByName( 'deadline' ).AsDateTime := someForm.DateTimePicker1.DateTime ;
someQuery.Params.ParamByName( 'status' ).AsString := someForm.Edit9.Text ;
someQuery.ExecSQL(true);

我不知道如何将包含 ID(第一个代码示例)的 WHERE 子句转换为 parameters(第二个代码示例) 还没有弄清楚,我似乎无法在谷歌上找到关于如何在 parameters 中使用 WHERE 的示例。 我对使用参数相当陌生。

我应该在 Params.ParamsByName( 'id' ) 之后写什么来获取 id?

服务器是 MYSQL。 如果我遗漏了任何内容,请在评论中告诉我,我会进行编辑

提前致谢!

最佳答案

很高兴您决定从字符串连接切换到参数绑定(bind),但这并不意味着您可以更改 UPDATE 语法。您仍然受 documented syntax 的约束为此

'UPDATE `someDBname`.`someTABLEname` SET client=:client, phone=:phone, email=:email, details=:details, specials=:specials, price=:price, address=:address, deadline=:deadline, status=:status WHERE id=:id';

这与您的第一个查询中的语法几乎相同,但您使用占位符代替字符串连接。然后你一个一个绑定(bind)参数

someQuery.Params.ParamByName( 'client' ).AsString := someForm.Edit1.Text ;
someQuery.Params.ParamByName( 'telefon' ).AsString := someForm.Edit2.Text ;

关于mysql - 如何在sql参数中使用WHERE ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40732612/

相关文章:

php - 为什么我的 MySQL 字段在更新时重置为 null?

php - 使用 UTC 时间返回 mysql 日期时间为几小时前、几天前等?

php - mysql 获取另一个表中email对应的数据

sql - 如何从sql到vb获得动态数据透视表

mysql - 按连接表值选择

c# - 在 C# 中使用可变参数编号调用命名参数

mysql - 删除重复的用户元

c# - 关于SQL注入(inject)和参数化查询的问题

javascript - 带参数的 WebApi 404 - 通过 javascript GET 调用

java - 如何在 Java 中调用泛型枚举的实现方法?