with AdoQry do //AdoQry为TADOQuery begin close; sql.clear; sql.add('select * from chaxun where 姓名='+''''+Edit1.Text+''''); open;
if IsEmpty then begin append; edit; FieldByName('姓名').AsString:=Edit1.Text; FieldByName('性别').AsString:=Edit2.Text; FieldByName('出生日期').AsDateTime:=StrToDatetime(Edit3.Text); post; end else begin Edit2.Text:=FieldByName('性别').AsString; Edit3.Text:=FieldByName('出生日期').AsString; end; end;//另外如果查询出的结果多于一条,你可以把Edit2和Edit3换成ComboBox,然后把数据写到下拉表去让用户自己选择
with AdoQry do begin close; sql.clear; sql.add('delete chaxun where '); open; end;
button的click事件: 你是查詢數據,不要加這一句:adoquery1.execsql;查詢: adoquery1.close; adoquery1.sql.txt:='select 姓名 from chaxun where 姓名="'+edit1.text+'"'; adoquery1.open; adoquery1.first; ......清空:with AdoQry do begin close; sql.clear; sql.add('delete from chaxun where 條件 '); open; end;
问题2:(如果不存在 那么将该人的姓名 性别 出生日期 数据进数据库 ),性别,出生日期 从哪里取数据,
不要告诉我,也是在EDIT2 EDIT3里吧,莫非点BUTTON1之前,姓名,性别,出生日期都在先在EDIT1,EDIT2,EDIT3先录入号
按下button后,先判断姓名是否在数据库中存在,如果存在就显示,不存在就存入数据库
代码如下
添加一个adoquery控件button的click事件
adoquery1.close;
adoquery1.sql.txt:='select 姓名 from chaxun where 姓名="'+edit1.text+'"';
adoquery1.execsql;
adoquery1.open;
adoquery1.first;while not adoquery1.eof then begin
edit2.text:=adoquery1.FieldValues['性别'];
edit3.text:=adoquery1.fieldvalues['日期'];
exit;
end;adoquery1.close;
adoquery1.SQL.Text:='insert into chaxun(姓名,性别,日期) values (:a,:b,:c)';
adoquery1.Parameters.ParamByName('a').Value:=trim(edit1.Text);
adoquery1.Parameters.ParamByName('b').Value:=trim(edit2.Text );
adoquery1.Parameters.ParamByName('c').Value:=trim(edit3.Text );
adoquery1.execsql;
第二段的意思是:数据当前数据指针是否是结尾(adoquery1.eof),如果不是结尾,那说明能查到edit1.text的数据,说明数据已经存在,在edit2,edit3上显示就成。(姓名一定要是唯一,如果不是唯一的话,只能显示第一个同名的人的数据)
第三段的意思是:如果数据库中不存在,进行数据添加操作
begin
close;
sql.clear;
sql.add('select * from chaxun where 姓名='+''''+Edit1.Text+'''');
open;
if IsEmpty then
begin
append;
edit;
FieldByName('姓名').AsString:=Edit1.Text;
FieldByName('性别').AsString:=Edit2.Text;
FieldByName('出生日期').AsDateTime:=StrToDatetime(Edit3.Text);
post;
end
else begin
Edit2.Text:=FieldByName('性别').AsString;
Edit3.Text:=FieldByName('出生日期').AsString;
end;
end;//另外如果查询出的结果多于一条,你可以把Edit2和Edit3换成ComboBox,然后把数据写到下拉表去让用户自己选择
begin
close;
sql.clear;
sql.add('delete chaxun where ');
open;
end;
你是查詢數據,不要加這一句:adoquery1.execsql;查詢:
adoquery1.close;
adoquery1.sql.txt:='select 姓名 from chaxun where 姓名="'+edit1.text+'"';
adoquery1.open;
adoquery1.first;
......清空:with AdoQry do
begin
close;
sql.clear;
sql.add('delete from chaxun where 條件 ');
open;
end;