简体   繁体   English

delphi唯一性中的UPDATE和DELETE代码

[英]Code for UPDATE and DELETE in delphi uniquery

I'm trying to update and delete my record. 我正在尝试更新和删除我的记录。 I'm using dbgrid as to show the database and i use uniquery to do the query. 我正在使用dbgrid来显示数据库,并且我使用uniquery做查询。 I managed to do the insert query but not with the update and delete. 我设法做插入查询,但没有更新和删除。

Here is my code : 这是我的代码:

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.

Thanks! 谢谢!

You're using the wrong syntax for your query. 您使用的查询语法错误。
The query does not use Delphi syntax and := does not make sense in that context. 该查询不使用Delphi语法,并且:=在这种情况下没有意义。

Change the query to: 将查询更改为:

UniQuery1.SQL.Add('UPDATE barang SET id= :i, name= :nam, stock = :st where id= :i');

The : is a prefix that tells TQuery that these are named parameters. :是一个前缀,告诉TQuery这些是命名参数。
Furthermore it makes little sense to set id = :i where id = :i that's a no-op. 此外, set id = :i where id = :i几乎没有意义, set id = :i where id = :i是无操作的。
So you can simplify the query to: 因此,您可以将查询简化为:

UniQuery1.SQL.Add('UPDATE barang SET name= :nam, stock = :st where id= :i');

In addition you don't have to insert/edit the queries. 另外,您不必插入/编辑查询。
These methods do not do what you think they do. 这些方法不会执行您认为的操作。

The insertion and editing is already being done by your SQL statements. 您的SQL语句已经完成了插入和编辑。
Don't use SQL.Add . 不要使用SQL.Add It's slow and error prone, because if there is already text in your SQL the added text will clash with the SQL that's already there. 它很慢且容易出错,因为如果您的SQL中已经有文本,则添加的文本将与已经存在的SQL冲突。
Never use SQL.Add ever again. 永远不要使用SQL.Add 。再次添加。

Change the first method like so: 像这样更改第一个方法:

procedure TForm1.Button4Click(Sender: TObject);
begin
      UniQuery1.SQL.Text:= 'UPDATE barang SET 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;

This method does not make any sense. 这种方法没有任何意义。

procedure TForm1.Button5Click(Sender: TObject);
begin
  UniQuery1.Insert;  //insert what? A query is not a table.
  UniQuery1.FieldByName('ID').AsString := Edit1.Text;
  UniQuery1.FieldByName('Name').AsString := Edit2.Text;
  UniQuery1.FieldByName('Stock').AsString := Edit3.Text;
  UniQuery1.Post;  //makes no sense here.
end;

Just replace this with a INSERT INTO.... sql statement. 只需将其替换为INSERT INTO.... sql语句即可。

Finally the last method should look like: 最后,最后一个方法应如下所示:

procedure TForm1.Button6Click(Sender: TObject);
begin
  UniQuery1.SQL.Text:= 'DELETE FROM barang where id=:i';
  UniQuery1.ParamByName('i').AsString:=edit1.Text;
  UniQuery1.ExecSQL;
end;

Surely you've figured out the there is no method called SQLdelete? 您确定已经确定没有所谓的SQLdelete方法吗?

You need to rethink the concept. 您需要重新考虑这个概念。
It's the SQL statement that does the work. 这是SQL语句完成的工作。
The Query only cares if the statement is a select -> if so so Query.Open . 如果语句是Query.Open >,则Query只在乎,如果这样,则Query.Open
Or if it will change the data (delete/insert/update) -> so Query.ExecSQL . 或者,如果它将更改数据(删除/插入/更新)-> Query.ExecSQL
All the rest is done in the SQL.Text . 其余所有操作均在SQL.Text完成。

Query.Edit etc 查询。编辑等
Yes you can do Query.Edit. 是的,您可以执行Query.Edit。
This puts the dataset in edit mode and allows the user to change fields in the query. 这会将数据集置于编辑模式,并允许用户更改查询中的字段。 The database layer will then transmit these changes to the underlying database tables. 然后,数据库层将这些更改传输到基础数据库表。
However this only works if the query is simple. 但是,这仅在查询简单的情况下有效。 If not it will silently break and not update your tables. 如果没有,它将无声地中断并且不会更新您的表。
Only use edit/insert/delete/post/cancel etc with Tables . 仅对Tables使用edit/insert/delete/post/cancel等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM