如何使用SQL将实际值插入表中?

如何使用SQL将实际值插入表中?,sql,delphi,Sql,Delphi,我正试图用以下代码将值插入MedicalationPrices表 procedure TForm1.btnAddMedicineClick(Sender: TObject); var sMedication, sQuantity : string; rPrice : real; begin sMedication := InputBox('Add Medication','Please enter the medications name',''); sQuantity := InputBo

我正试图用以下代码将值插入MedicalationPrices表

procedure TForm1.btnAddMedicineClick(Sender: TObject);
var
 sMedication, sQuantity : string;
 rPrice : real;
begin
sMedication := InputBox('Add Medication','Please enter the medications name','');
sQuantity := InputBox('Add Medication','Please enter the the quantity','');
rPrice := StrToFloat(InputBox('Add Medication','Please enter the the price',''));

with dmHospital do
begin
  qryPrices.SQL.Clear;
  qryPrices.SQL.Add('INSERT INTO MedicationPrices (Medication, Quantity)');
  qryPrices.SQL.Add('VALUES(' + QuotedStr(sMedication) +',' + QuotedStr(sQuantity)  + ' )');
  qryPrices.Parameters.ParamByName('Price').Value := rPrice;
  qryPrices.ExecSQL;
  qryPrices.SQL.Clear;
  qryPrices.SQL.Text := 'SELECT * MedicationPrices ';
  qryPrices.Open;
end;
end;
然而,它和一些不同的变体根本不起作用。我得到:

我不明白为什么它没有看到“价格”,因为它在表中已经很清楚了。

您应该在查询行中添加带有值的参数

然后,当您使用ParamByName函数时,它将基本上用您设置的rPrice值替换查询中的参数:Price

更正示例:

with dmHospital do
begin
  qryPrices.SQL.Clear;
  qryPrices.SQL.Add('INSERT INTO MedicationPrices (Medication, Quantity, Price)');
  qryPrices.SQL.Add('VALUES(:Medication, :Quantity, :Price)');
  qryPrices.Parameters.ParamByName('Medication').Value := sMedication;
  qryPrices.Parameters.ParamByName('Quantity').Value := sQuantity;
  qryPrices.Parameters.ParamByName('Price').Value := rPrice;
  qryPrices.ExecSQL;
  qryPrices.SQL.Clear;
  qryPrices.SQL.Text := 'SELECT * FROM MedicationPrices ';
  qryPrices.Open;
end;

另请参见INSERT中的关于Delphi中的参数。

SELECT语句中似乎缺少一个FROM。很好,catch@MartynA,我没有检查其余的代码。我只是编辑了我的答案-@brclz感谢您的回答,但我仍然收到相同的错误消息,但改为:“药物治疗”。@OceanKnight:真的!语法有点错误,它的ParamByName'PARAMETER_NAME'。值没有两个点。我更新了它。我认为您缺少了列又名字段名和查询参数之间的区别。他们不是一回事。我不想在这家医院里呆着:-/和..应该永远禁止doimo@JerryDodge谢谢你投的信任票。幸运的是,这不是一家真正的医院。@AlbertoMiola为什么?它使编码更短,因此更快。