有什么理由不应该这样做吗?SQL不会将记录添加到数据库



所以我有另一个问题让我很惊讶为什么它不起作用,但是你们都比我聪明,所以…

代码应该添加新的记录到我的数据库,这是一个预算程序,它问用户的收入收到的日期,用户的ID,收入的名称和类别和收到的金额,我使用SQL将其添加到一个ms访问数据库。看一看,请帮忙!

代码如下:

procedure TForm1.BitBtn8Click(Sender: TObject);
var
 iNameOfIncome, iCategory, iDate :integer;
 sDate, sAmount :string;
 dDate :TDate;
begin
 with dmRecords do
  begin
   sAmount := Edit7.Text;
   iNameOfIncome := ComboBox10.ItemIndex + 1;
   iCategory := ComboBox2.ItemIndex + 1;
   dDate := DateTimePicker1.Date;
   sDate := DateToStr(dDate);
   qryRecords.SQL.Clear;
   qryRecords.Active := False;
   qryRecords.SQL.Add('INSERT INTO [Income (Ledger)](DateofIncome, [User ID], [Income ID], [Category ID], [Income Amount]');
   qryRecords.SQL.Add('VALUES (' + sDate + ',' + IntToStr(SpinEdit2.Value) + ',' + IntToStr(iNameOfIncome) + ',' + IntToStr(iCategory) + ',' + sAmount + ')');
   qryRecords.ExecSQL;
   qryRecords.SQL.Clear;
   qryRecords.SQL.Add('SELECT * FROM [Income (Ledger)] ORDER BY IncomeNo');
   qryRecords.Active := True;
  end;  

提前感谢!

我认为你应该这样重写

  qryRecords.Close;
  qryRecords.SQL.Text :=
  'INSERT INTO [Income (Ledger)](DateofIncome, [User ID], [Income ID], [Category ID], [Income Amount]) ' +
  'VALUES (:DateofIncome,:User_ID,:Category_ID,:Income_ID,:Income_Amount)';
  qryRecords.ParamByName('DateofIncome').AsDateTIme := dDate;
  qryRecords.ParamByName('User_ID').AsInteger := SpinEdit2.Value;
  qryRecords.ParamByname('Category_ID').AsInteger := iCategory;
  qryRecords.ParamByname('Income_ID').AsInteger := iNameOfIncome;  
  qryRecords.ParamByname('Income_Amount').AsFloat:= sAmount;
  qryRecords.ExecSQL;
  qryRecords.SQL.Text := 'SELECT * FROM [Income (Ledger)] ORDER BY IncomeNo';
  qryRecords.Open;

这样你将传递正确的类型给DB。否则,您不知道db将如何解释您发送给它的内容。

最新更新