请教有关SQL语句使用的问题,会的朋友请关注一下。(45分)

阿虫

Unregistered / Unconfirmed
GUEST, unregistred user!
我的窗口上有个EDIT,我通过它给数据库输入值表号的记录。
我希望当输入的数值在数据库中存在是,出现一个提示,询问是覆盖还是放弃。
可下面这段程序编译时通过了,一执行老提示不在编辑或插入状态,为什么。
那位老兄会,教教俺,我搞了几天了,也没搞好。

procedure TForm1.Button1Click(Sender: TObject);

begin
query1.SQL.clear;
query1.sql.add('select * from yanshi where bh=:key_0');//BH是主键,也是我要判断
//是否重复的值。
query1.requestlive:=true;
query1.Params[0].asstring:=edit1.text;//EDIT1是我用来输入BH的文本框,其它字段记录
//也有相应的EDIT输入其值。
query1.active:=true;
if query1.recordcount=0 then
begin
query1.append;
end

else
begin
if application.messagebox(pchar('数据重复'),'error',mb_yesno+mb_iconquestion)=idyes then
query1.Edit;
end;
if query1.state in [dsedit,dsinsert] then
begin
query1.sql.clear;
query1.sql.add('insert into yanshi(xuh,jh) values(''22'',''供电'')');//我想在这里输入其它的字段内容,其中的字段XUH,JH,

end;

end;
 
if application.messagebox(pchar('数据重复'),'error',mb_yesno+mb_iconquestion)=idyes then
query1.Edit;
end;
if query1.state in [dsedit,dsinsert] then
begin
query1.sql.clear; //----这句将使query1处于浏览状态
query1.sql.add('insert into yanshi(xuh,jh) values(''22'',''供电'')');//我想在这里输入其它的字段内容,其中的字段XUH,JH,

end;


你是踏雪无痕?
 
去掉的话好象也不行吗?
 
建议另外用一个QUERY来完成INSERT操作。
 
//您即然用了Edit和Append,就不要用Insert 语句了

//将query1 的ReauestLive属性设为True,最好也将cahceUpdate属性设为True
//将一个UpdateSql放上,将Query的UpdateObject设为该UpdateSQL控件
with Query1 do
begin
Close;
SQL.Text := 'select * from yanshi where bh=' + QuotedStr(Edit1.Text);
Open;
if not IsEmpty then
begin
if Application.MessageBox('数据重复', '提示', MB_YesNo + MB_ICONQUESTION) = mrYes then
Edit;
end
else
Append;
if State in [dsEdit, dsInsert] then
begin
FieldByName('XUH').AsString := '22';
FieldByName('JH').AsString := '供电';
Post;
ApplyUpdates; {如果cacheUpdate为True则用此句}
end;
end;
 
好久没来看了,以为没人给我回答了,不过还是发现有几个朋友帮我解答了,真的谢谢了。
 
我按SWV0506老兄的写了,编译时通过了,可执行时出错,提示没SQL语句申明。为什么呀,
我就这么一个小问,可这么长时间也没搞定,SQL语句真的这么深不可测吗?
 
顶部