当前位置:编程学习 > Delphi >>

delphi中的SQL语句

delphi中,如何在某个条件(如在序号在库中有记录)下执行修改数据库的语句,在另一条件(序号在库中没有记录)下执行插入数据库的语句
补充:adocommand1.CommandText:=' update 库存库 set 库存数量=库存数量+'+input.stringgrid1.Cells[3,i]+',库存金额=库存金额+'+input.stringgrid1.Cells[5,i]+' where 货号='+stringgrid1.Cells[1,i]+' ';
adocommand1.Execute;

adocommand1.CommandText:= 'insert into 库存库([货号],[库存数量],[库存单价],[库存金额]) values('''+stringgrid1.Cells[1,i]+''','''+input.stringgrid1.Cells[3,i]+''','''+input.stringgrid1.Cells[4,i]+''','''+input.stringgrid1.Cells[5,i]+''')' ;
adocommand1.Execute;

当“货号”存在执行上面修改的语句,“货号”不存在时执行下面的插入语句
答案:

添加、修改前面加个条件。

with query1 do

begin

close;

sql.clear ;

sql.add('select * from yourtable where .序号=....');

open ;

end;

if query1.count=0 then

begin

你的插入语句;

end

else if query1.count>0 then

begin

你的修改语句;

end;

你可以先执行更新的然后判断一下影响行数如果为0就再执行插入的,
代码大概是这样的
adocommand1.Close;
adocommand1.CommandText:=' update 库存库 set 库存数量=库存数量+'+input.stringgrid1.Cells[3,i]+',库存金额=库存金额+'+input.stringgrid1.Cells[5,i]+' where 货号='+stringgrid1.Cells[1,i]+' ';
if adocommand1.Execute=0 then
begin
adocommand1.Close;
adocommand1.CommandText:= 'insert into 库存库([货号],[库存数量],[库存单价],[库存金额]) values('''+stringgrid1.Cells[1,i]+''','''+input.stringgrid1.Cells[3,i]+''','''+input.stringgrid1.Cells[4,i]+''','''+input.stringgrid1.Cells[5,i]+''')' ;
adocommand1.Execute;
end;
答案补充
adoquery1.SQL.Add('where 进货单明细.进货单号=进货单.编号 and  进货单明细.货号=商品清单.货号');
答案补充
前面什么问题啊你也没有说清楚啊
答案补充
最后一次补充回答了,你代码怎么写的是按我给你写的写的吗?
没有代码我怎么知道你那错了啊
无语。。。。。。。。。。。。。
var 
tf:tfilestream;
name,path:string;
p:integer;
begin
p:=0;
if form1.OpenDialog1.Execute then
begin
try
name:=form1.OpenDialog1.FileName;
form1.OpenDialog1.GetNamePath;
showmessage(name);
tf:=tfilestream.Create(name,fmOpenRead);
adoquery1.Edit;
adoquery1.Insert;
tblobfield(adoquery1.FieldByName('contect')).loadfromstream(tf);//contect为表中存储文件内容的字段名
p:=pos('.',name);
name:=copy(name,p,length(name)-p+1);
adoquery1.FieldByName('name').Value:=name;//name为表中存储文件后缀名的字段
adoquery1.Post;
tf.Free;
except
tf.Free;
end;

上一个:Utf8编码转换问题 Delphi
下一个:Delphi怎么起步呢啊?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,