!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我的意思是,判断edit的内容,添加where子句。 如果,所有的edit都为空,那么,最后的的是 'select * from table' zx_wang(wzx) 所写的: s:='select * from table where 1=1 '; 这个的话就必须要填写一个edit了,就是多了一个 ’where 1=1 ‘出来了。并非是我要的。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
注意你生成的S字符串中SQL语句的正确性,该有空格的就要加上 空格,该有括号的要有括号,你可能单步运行一下: begin s:='select * from table '; if edit1.text<>'' then s:=s+' where A1=('+edit1.text+') and '; else if edit2.text<>'' then s:=s+' where A1=('+edit1.text+') and '; ......
理论正确,只是差了一个加不加where 的问题,那就在s:='select * from table'之后加一个判断是否所有Edit是否都为空的语句: if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then s:= s+' where '; …… ……//追加文本框的内空
if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then s:= s+' where '; ……and 应该改成or吧
var s,ConStr:String; begin s:='select * from table '; ConStr:=''; if (Edit1.text<>'') then ConStr:=ConStr+'and A1='''+Edit1.Text+''' '; if (Edit2.Text<>'') then ConStr:=ConStr+'and A2='''+Edit2.Text+''' '; if (Edit3.Text<>'') then ConStr:=ConStr+'and A3='''+Edit3.Text +''' '; if ConStr<>'' then begin ConStr:=Copy(Constr,4,Length(ConStr)-3);//把第一个and 写成where ConStr:='Where'+ ConStr; S:=S+ConStr; end; showmessage(s);//显示出来验证一下 end;
A到1AX都是字符型的吗
把你生成的SQL语句放到SQL Explore里执行一下,看是什么错误
如果,所有的edit都为空,那么,最后的的是
'select * from table' zx_wang(wzx) 所写的:
s:='select * from table where 1=1 ';
这个的话就必须要填写一个edit了,就是多了一个
’where 1=1 ‘出来了。并非是我要的。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
空格,该有括号的要有括号,你可能单步运行一下:
begin
s:='select * from table ';
if edit1.text<>'' then
s:=s+' where A1=('+edit1.text+') and ';
else if edit2.text<>'' then
s:=s+' where A1=('+edit1.text+') and ';
......
这是我提出的问题,尽管美得到解决可是思路是对的。
http://www.csdn.net/expert/topic/648/648059.xml?temp=.1813928给我加点分,我穷得可怜。
if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
s:= s+' where ';
……
……//追加文本框的内空
if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
s:= s+' where ';
……and 应该改成or吧
s,ConStr:String;
begin
s:='select * from table ';
ConStr:='';
if (Edit1.text<>'') then
ConStr:=ConStr+'and A1='''+Edit1.Text+''' ';
if (Edit2.Text<>'') then
ConStr:=ConStr+'and A2='''+Edit2.Text+''' ';
if (Edit3.Text<>'') then
ConStr:=ConStr+'and A3='''+Edit3.Text +''' ';
if ConStr<>'' then
begin
ConStr:=Copy(Constr,4,Length(ConStr)-3);//把第一个and 写成where
ConStr:='Where'+ ConStr;
S:=S+ConStr;
end;
showmessage(s);//显示出来验证一下
end;
其实像zx_wang(wzx) 那样作是非常高明的,你不一定非要在所的Edit为空的时候就不要Where。
上面我写的程序中把所有的“If Edit.Text<>'' then”换成“If Trim(Edit1.Text)<>''”这样要好些
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我的意思是,判断edit的内容,添加where子句。
如果,所有的edit都为空,那么,最后的的是
'select * from table' zx_wang(wzx) 所写的:
s:='select * from table where 1=1 ';
这个的话就必须要填写一个edit了,就是多了一个
’where 1=1 ‘出来了。并非是我要的。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
加上一个 'where 1=1' 是个巧方法,不影响结果!!!!!!!!!!!!!!!!!! 还省去好多判断和字符串操作!!!!!!!!!!!!!
array[i]=[1..10];
if editi<>'' then
select * from tablename
where a1='''+editi.text+''';