数据库问题
检查有没有数据,有则修改,无则添加
- SQL code
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;修改
- SQL code
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
哪里错误啊?还有,我刚学数据库,这样写代码有错误吗?
[解决办法]
若要核实sql语句也不难:
ShowMsg(ADOQuery1.SQL.Text);