Delphi ADOQuery连接数据库的查询、插入、删除、修改

//查询记录
procedure  TForm1 . Button1Click(Sender: TObject);
begin
ADOQuery . Close;
ADOQuery . SQL . Clear;
ADOQuery . SQL . Add( 'select * from YourTABLE where 查询条件' );
ADOQuery . Open;
 
//插入记录
procedure  TForm1 . Button2Click(Sender: TObject);
begin
ADOQuery . Close;
ADOQuery . SQL . Clear;
ADOQuery . SQL . Text:= 'insert into YourTABLE(字段1,字段2) values(:字段1,:字段2)' ;
// ADOQuery.SQL.Add('insert into YourTABLE values(:字段1)');
ADOQuery . Parameters . ParamByName( '字段1' ).Value:=trim(Edit1 . Text);
ADOQuery . Parameters . ParamByName( '字段2' ).Value:=trim(Edit2 . Text);
ADOQuery . ExecSQL;
end ;
//删除记录
procedure  TForm1 . Button3Click(Sender: TObject);
begin
ADOQuery . Close;
ADOQuery . SQL . Clear;
ADOQuery . SQL . Text:= 'Delete from YourTABLE where 字段3=:字段3' ;
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Delete from NEW_TABLE where 字段3=:字段3');
ADOQuery . Parameters . ParamByName( '字段3' ).Value:=trim(Edit3 . Text);
ADOQuery . ExecSQL;
//删除记录也可用DeleteRecords()函数
procedure  DeleteRecords(AffectRecords: TAffectRecords = arAll);  
这个函数有一个参数:AffectRecords可以取如下的值:  
1 、arCurrent :删除当前记录  
2 、arFiltered :删除符合Filter过滤后的所有记录(如果你使用Filter过滤的话)  
3 、arAll          :删除所有记录  
4 、arAllChapters :Delete affects all chapters(ADO chapters)
//修改记录
procedure  TForm1 . Button4Click(Sender: TObject);
begin
ADOQuery . Close;
ADOQuery . SQL . Clear;
ADOQuery . SQL . Text:= 'Update YourTABLE SET 字段4=:字段4' ;
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Update YourTABLE SET 字段4=:字段4');
ADOQuery . Parameters . ParamByName( '字段4' ).Value:=trim(Edit4 . Text);
ADOQuery . ExecSQL;
//即时更新插入、删除、修改后的记录
在上面插入、删除、修改的语句后添加如下代码即可:
ADOQuery . Close;
ADOQuery . SQL . Add( 'select * from YourTABLE where 查询条件' );
ADOQuery . Open;
//使用ADOQuery时注意:

1、如果你需要更改数据,query.requestlive必须为true

2、如果有输入参数的时候,容易出错,通常的错法是这样:
比如:“WHERE abc = : abc”
改正如下:“WHERE abc=:abc”就是说=:前后都不能留空格。
3、ADOQuery.Open与ADOQuery.ExecSQL 有不同之处。
ADOQuery.Open一般用在查询,select时候;而ADOQuery.ExecSQL用在insert,delete,update等。

 

 

===========================================================

1、查询循环遍历数据

with adoquery1 do
begin

adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from 表名where 查询条件');
adoquery1.Open;

end;

遍历:

with adoquery1 do
begin
  close;
  sql.add('select * from 表名');
  prepared;
  open;


  first;
  while not eof do
  begin
     combobox1.items.add(fieldbyname('字段').asstring);  
     next;
  end; 

或者写成:

var
icount:integer;

icount:=adoquery1.recordcount;
for i:=1 to icount do
begin
   combobox1.items.add(fieldbyname('字段').asstring);  
   next;
end;

或者是:

with adoquery1 do

  begin

     recordset.movefirst;

     while not recordset.eof do

     begin

        combobox1.items.add(recordset.fields['字段'].value.asstring); 

         recordset.movenext;

   end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Add('select * from YourTABLE where 查询条件');
ADOQuery.Open;

end;


2、ADOQuery插入

with ADOQuery  do

    Close;
    SQL.Clear;
    SQL.Text:='insert into 表名(字段1,字段2) values(:字段1,:字段2)';
    Parameters.ParamByName('字段1').Value:=trim(Edit1.Text);
    Parameters.ParamByName('字段2').Value:=trim(Edit2.Text);
    ExecSQL;

end

 

或者直接写成:

sql :='insert into 表 values(ID,loginn,username1,password1,quanxian14,quanxian2,remark)values ('+trim(Edit1.Text);+','+...(后面你自己补)ADOQuery1.SQL.Text := sql;

ADOQuery1.ExecSQL;


//插入记录

procedure TForm1.Button2Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='insert into 表名(字段1,字段2) values(:字段1,:字段2)';
// ADOQuery.SQL.Add('insert into 表名 values(:字段1)');
ADOQuery.Parameters.ParamByName('字段1').Value:=trim(Edit1.Text);
ADOQuery.Parameters.ParamByName('字段2').Value:=trim(Edit2.Text);
ADOQuery.ExecSQL;
end;

 

3、删除

procedure TForm1.Button3Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='Delete from 表名 where 字段3=:字段3';
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Delete from 表名 where 字段3=:字段3');
ADOQuery.Parameters.ParamByName('字段3').Value:=trim(Edit3.Text);
ADOQuery.ExecSQL;

//删除记录也可用DeleteRecords()函数

procedure DeleteRecords(AffectRecords: TAffectRecords = arAll); 
这个函数有一个参数:AffectRecords可以取如下的值: 
1、arCurrent :删除当前记录 
2、arFiltered :删除符合Filter过滤后的所有记录(如果你使用Filter过滤的话) 
3、arAll :删除所有记录 
4、arAllChapters :Delete affects all chapters(ADO chapters)

 

4、修改

procedure TForm1.Button4Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='Update 表名 SET 字段4=:字段4';
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Update 表名 SET 字段4=:字段4');
ADOQuery.Parameters.ParamByName('字段4').Value:=trim(Edit4.Text);
ADOQuery.ExecSQL;

 

5、即时更新插入、删除、修改后的记录

在上面插入、删除、修改的语句后添加如下代码即可:
ADOQuery.Close;
ADOQuery.SQL.Add('select * from 表名 where 查询条件');
ADOQuery.Open;

//使用ADOQuery时注意:

http://blog.csdn.net/draling/article/details/7875179

转载于:https://www.cnblogs.com/chuangyiyuan/p/6043601.html

更多推荐