[英]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
的主鍵的數據類型,或者您正在識別單個產品並調整參數屬性DataType
和Value
,以及 SQL 命令中的標識符"product_key"
!
您還必須為quantity_delivered
參數值插入一個變量,因為您肯定不想每次都將數量減少42。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.