delphi唯一性中的UPDATE和DELETE代码 [英] Code for UPDATE and DELETE in delphi uniquery

查看:159
本文介绍了delphi唯一性中的UPDATE和DELETE代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新和删除我的记录. 我正在使用dbgrid来显示数据库,并且我使用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. I managed to do the insert query but not with the update and delete.

这是我的代码:

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.

谢谢!

推荐答案

您在查询中使用了错误的语法.
该查询不使用Delphi语法,并且:=在这种情况下没有意义.

You're using the wrong syntax for your query.
The query does not use Delphi syntax and := does not make sense in that context.

将查询更改为:

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

:是一个前缀,告诉TQuery这些是命名参数.
此外,对于set id = :i where id = :i这是无操作,这没有什么意义.
因此,您可以将查询简化为:

The : is a prefix that tells TQuery that these are named parameters.
Furthermore it makes little sense to set id = :i where id = :i that's a no-op.
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.

您的SQL语句已经完成了插入和编辑.
不要使用SQL.Add.这很慢且容易出错,因为如果您的SQL中已经有文本,则添加的文本将与已经存在的SQL冲突.
永远不要再使用SQL.Add.

The insertion and editing is already being done by your SQL statements.
Don't use 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.
Never use SQL.Add ever again.

像这样更改第一个方法:

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;

此方法没有任何意义.

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;

只需将其替换为INSERT INTO.... sql语句.

Just replace this with a INSERT INTO.... sql statement.

最后,最后一个方法应类似于:

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;

您确定已经找到了没有称为SQLdelete的方法吗?

Surely you've figured out the there is no method called SQLdelete?

您需要重新考虑这个概念.
这是SQL语句完成的工作.
查询仅关心语句是否为select->,如果是,则Query.Open.
或者,如果它会更改数据(删除/插入/更新)->则Query.ExecSQL.
其余所有操作均在SQL.Text中完成.

You need to rethink the concept.
It's the SQL statement that does the work.
The Query only cares if the statement is a select -> if so so Query.Open.
Or if it will change the data (delete/insert/update) -> so Query.ExecSQL.
All the rest is done in the SQL.Text.

查询.编辑等
是的,您可以执行Query.Edit.
这会将数据集置于编辑模式,并允许用户更改查询中的字段.然后,数据库层会将这些更改传输到基础数据库表.
但是,这仅在查询简单的情况下有效.如果不是这样,它将无声地中断并且不会更新您的表.
只能将edit/insert/delete/post/cancel等与Tables一起使用.

Query.Edit etc
Yes you can do 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.

这篇关于delphi唯一性中的UPDATE和DELETE代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆