簡體   English   中英

如何在 Delphi (adotable) 中使用 SQL 查詢更新 DBGrid 中的 Quantity 字段?

[英]How can I update the Quantity field in DBGrid using SQL query in in Delphi (adotable)?

我在 table1(產品)中有一個字段(quantity),在 table2(Sale)中有一個字段(quantity_delivered)。 每次我銷售產品時,必須更新 table1 中的數量(quantity-quantity_delivered)。

我怎么能在德爾福做到這一點? nedd 想法幫助

使用帶有 SQL 語句的 TAdoQuery 組件,如下所示:

ADOQuery.SQL.Text :=
'UPDATE product'
+ ' SET quantity = quantity - :quantity_delivered'
+ ' WHERE product_key = :product_key';

ADOQuery.Parameters.ParamByName('quantity_delivered').DataType := ftInteger;
ADOQuery.Parameters.ParamByName('quantity_delivered').Value := 42;

ADOQuery.Parameters.ParamByName('product_key').DataType := ftInteger;
ADOQuery.Parameters.ParamByName('product_key').Value := 123;

ADOQuery.ExecSQL;

此代碼使用 SQL 參數。 這有助於防止 SQL 注入攻擊,因為參數值已正確轉換和轉義。

由於您沒有提供有關表格布局的太多細節,因此我不得不編寫更多示例來說明如何做到這一點!
請檢查表product的主鍵的數據類型,或者您正在識別單個產品並調整參數屬性DataTypeValue ,以及 SQL 命令中的標識符"product_key"
您還必須為quantity_delivered參數值插入一個變量,因為您肯定不想每次都將數量減少42。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM