我正在尝试更新和删除我的记录。我正在使用 dbgrid 来显示数据库,并且我使用 uniquery 来进行查询。我设法进行了插入查询,但没有进行更新和删除。
这是我的代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Data.DB,
DBAccess, Uni, UniProvider, MySQLUniProvider, MemDS, Vcl.StdCtrls, DAScript,
UniScript;
type
TForm1 = class(TForm)
UniConnection1: TUniConnection;
MySQLUniProvider1: TMySQLUniProvider;
UniDataSource1: TUniDataSource;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Label3: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Label4: TLabel;
DBGrid1: TDBGrid;
UniQuery1: TUniQuery;
UniScript1: TUniScript;
procedure Button1Click(Sender: TObject);
procedure DBGrid1CellClick(Column: TColumn);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Application.Terminate();
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
UniQuery1.Edit;
UniQuery1.SQL.Add('UPDATE barang SET id:=i, name:=nam, stock:=st where id=:i');
UniQuery1.ParamByName('i').AsString := Edit1.Text;
UniQuery1.ParamByName('nam').AsString := Edit2.Text;
UniQuery1.ParamByName('st').AsString := Edit3.Text;
UniQuery1.ExecSQL;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
UniQuery1.Insert;
UniQuery1.FieldByName('ID').AsString := Edit1.Text;
UniQuery1.FieldByName('Name').AsString := Edit2.Text;
UniQuery1.FieldByName('Stock').AsString := Edit3.Text;
UniQuery1.Post;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
UniQuery1.Edit;
UniQuery1.SQLdelete('DELETE FROM barang where id=:i');
UniQuery1.ParamByName('i').AsString:=edit1.Text;
UniQuery1.ExecSQL;
end;
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
edit1.Text := DBGrid1.Fields[0].asstring;
edit2.text := DBGrid1.Fields[1].asstring;
edit3.Text := DBGrid1.Fields[2].asstring;
end;
end.
谢谢!