检查有没有数据,有则修改,无则添加
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString :='select * from session where type=1 and Owner='+quotedstr(templogin.Accounts)+' and Name='+quotedstr(TempLogin.Name);
SQL.Add(SQLString);
Open;
end;
if ADOQuery1.RecordCount<>0 then
begin
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString:='UPDate Session SET LocalIP='+quotedstr(TempLogin.RemoteIP)+', LocalPort='+(IntToStr(TempLogin.RemotePort))+
', EngineServerIP='+quotedstr(TempLogin.ServerIP)+', EngineServerPort='+IntToStr(TempLogin.ServerPort)+', Online='+inttostr(1)+
' where type=1 and owner='+quotedstr(TempLogin.Accounts)+' and name='+quotedstr(TempLogin.Name);
SQL.Add(SQLString);
ExecSQL;
end;
end
else
begin
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString :='Insert into Session (type, Owner, Name, LocalIP, LocalPort, EngineServerIP, EngineServerPort, Online) VALUES '+'('+
IntToStr(1)+','+quotedstr(TempLogin.Accounts)+','+quotedstr(TempLogin.Name)+','+quotedstr(TempLogin.RemoteIP)+','+
IntToStr(TempLogin.RemotePort)+','+quotedstr(TempLogin.ServerIP)+','+inttostr(TempLogin.RemotePort)+','+inttostr(1)+')';
SQL.Add(SQLString);
ExecSQL;
end;
end;
修改
with ADOQuery1 do
begin
Close;
SQLstring :='update session set Online='+inttostr(0)+'where Owner='+quotedstr(Accounts)+'and Name='+quotedstr(RName);
SQL.Add(SQLstring);
ShowMsg(SQLstring);
ExecSQL;
end;
为什么修改数据的数据的时候,会自动添加一行呢?就等于又执行了一次:UPDate Session SET
哪里错误啊?还有,我刚学数据库,这样写代码有错误吗?
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString :='select * from session where type=1 and Owner='+quotedstr(templogin.Accounts)+' and Name='+quotedstr(TempLogin.Name);
SQL.Add(SQLString);
Open;
end;
if ADOQuery1.RecordCount<>0 then
begin
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString:='UPDate Session SET LocalIP='+quotedstr(TempLogin.RemoteIP)+', LocalPort='+(IntToStr(TempLogin.RemotePort))+
', EngineServerIP='+quotedstr(TempLogin.ServerIP)+', EngineServerPort='+IntToStr(TempLogin.ServerPort)+', Online='+inttostr(1)+
' where type=1 and owner='+quotedstr(TempLogin.Accounts)+' and name='+quotedstr(TempLogin.Name);
SQL.Add(SQLString);
ExecSQL;
end;
end
else
begin
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQLString :='Insert into Session (type, Owner, Name, LocalIP, LocalPort, EngineServerIP, EngineServerPort, Online) VALUES '+'('+
IntToStr(1)+','+quotedstr(TempLogin.Accounts)+','+quotedstr(TempLogin.Name)+','+quotedstr(TempLogin.RemoteIP)+','+
IntToStr(TempLogin.RemotePort)+','+quotedstr(TempLogin.ServerIP)+','+inttostr(TempLogin.RemotePort)+','+inttostr(1)+')';
SQL.Add(SQLString);
ExecSQL;
end;
end;
修改
with ADOQuery1 do
begin
Close;
SQLstring :='update session set Online='+inttostr(0)+'where Owner='+quotedstr(Accounts)+'and Name='+quotedstr(RName);
SQL.Add(SQLstring);
ShowMsg(SQLstring);
ExecSQL;
end;
为什么修改数据的数据的时候,会自动添加一行呢?就等于又执行了一次:UPDate Session SET
哪里错误啊?还有,我刚学数据库,这样写代码有错误吗?
var recount:integer;
begin
with ADOQuery1 do
begin
Close;
SQL.Text:='select * from session where type=1 and Owner='+quotedstr(templogin.Accounts)+' and Name='+quotedstr(TempLogin.Name);
Open;
recount:=RecordCount;
Close;
if recount>0 then
SQL.Text:='UPDate Session SET LocalIP='+quotedstr(TempLogin.RemoteIP)+', LocalPort='+(IntToStr(TempLogin.RemotePort))+
', EngineServerIP='+quotedstr(TempLogin.ServerIP)+', EngineServerPort='+IntToStr(TempLogin.ServerPort)+', Online='+inttostr(1)+
' where type=1 and owner='+quotedstr(TempLogin.Accounts)+' and name='+quotedstr(TempLogin.Name)
else
SQL.Text:='Insert into Session (type, Owner, Name, LocalIP, LocalPort, EngineServerIP, EngineServerPort, Online) VALUES '+'('+
IntToStr(1)+','+quotedstr(TempLogin.Accounts)+','+quotedstr(TempLogin.Name)+','+quotedstr(TempLogin.RemoteIP)+','+
IntToStr(TempLogin.RemotePort)+','+quotedstr(TempLogin.ServerIP)+','+inttostr(TempLogin.RemotePort)+','+inttostr(1)+')';
ExecSQL;
end;这样会简洁、易读。
ShowMsg(ADOQuery1.SQL.Text);