您的位置:首页 > 编程语言 > Delphi

delphi学习笔记_查询、插入、删除、修改

2012-08-16 22:43 405 查看
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时注意:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: