毕业设计啊毕业设计好久之前学的delphi已经忘记的差不多了 sql就更不用说了 请教大家一个问题:我要在access的一个表中添加一组数据。完成之后下次再添加这组数据的时候我们知道就会出错。请问如何判断好呢?只要判断主键是否存在就可以了请给代码 我写的代码错的。。
------------------------------------------------------------------
procedure Taddnew.addClick(Sender: TObject);
{var
i:integer;
begin
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * form info where username:='''+edit1.Text+'''');
adoquery1.Open ;
i:=adoquery1.RecordCount;
if i<>0 then }
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('insert into info(username,phoneno,cellphoneno,email,re) values('''+edit1.text+''','''+edit2.text+''','''+edit3.text+''','''+edit4.text+''','''+edit5.text+''')');
adoquery1.execsql;
showmessage('信息添加完成!');
{ end
else
showmessage('用戶已经存在!');
adoquery1.close; }
end;
------------------------------------------------------------------
procedure Taddnew.addClick(Sender: TObject);
{var
i:integer;
begin
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * form info where username:='''+edit1.Text+'''');
adoquery1.Open ;
i:=adoquery1.RecordCount;
if i<>0 then }
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('insert into info(username,phoneno,cellphoneno,email,re) values('''+edit1.text+''','''+edit2.text+''','''+edit3.text+''','''+edit4.text+''','''+edit5.text+''')');
adoquery1.execsql;
showmessage('信息添加完成!');
{ end
else
showmessage('用戶已经存在!');
adoquery1.close; }
end;
adoquery1.Open ;
if Not adoquery1.IsEmpty then ShowMessage('用户已经存在');
如果表里 username 字段是唯一的话procedure Taddnew.addClick(Sender: TObject);
var
i:integer;
begin
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('select * form info where username:='+QuotedStr(edit1.Text));
adoquery1.Open ;
i:=adoquery1.RecordCount;
if i=0 then
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('insert into info(username,phoneno,cellphoneno,email,re) '+
' values('+QuotedStr(edit1.text)+','+QuotedStr(edit2.text)+','+QuotedStr(edit3.text)+','+
QuotedStr(edit4.text)+','+QuotedStr(edit5.text)+')');
adoquery1.execsql;
showmessage('信息添加完成!');
end
else showmessage('用戶已经存在!');
AdoQuery1.close;
end;
adoquery1.Open ;
if Not adoquery1.IsEmpty then ShowMessage('用户已经存在');
-----------------------------------------------------------------------------
这样会出错的haowanle() 你的代码还不跟我的一样的 就if那里改变了一下 。希望再次指教
2.程序控制不让数据库的系统错误出现在用户前
sqlstr:='select count(*) as num from info where username='''+trim(edit1.text)+'''';
with adoquery1 do
begin
close;
sql.clear;
sql.add(sqlstr);
open; if fieldbyname('num').value>0 then //这边不知道是value还是asInteger了
showmessage('用户已存在')
else
showmessage('用户不存在');
end;