>>>>>>>>这么简单的查询语句,我就是写不出来!!<<<<<<<<<<<

1999 2002-04-15 05:13:23
在form上有12个edit,要根据edit的内容,动态的生产sql语句。
我先定义了一个s:string 。
begin
s:='select * from table ';
if edit1.text<>'' then
s:=s+'where A1='''+edit1.text+''''
else
……
就是每个edit的内容作为where子句的查询条件,但是,像我上面写的话,好像是不行的。
就是我最后生成的查询语句是这样的形式:
select * from table where A1='edit1.text' and A2='edit2.text' and Ax='editx.text';
我该如何写?并且,and是不是可以无限级的使用?
...全文
50 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cookieyfeng 2002-04-15
  • 打赏
  • 举报
回复
定义一个数组i
array[i]=[1..10];
if editi<>'' then
select * from tablename
where a1='''+editi.text+''';
1999 2002-04-15
  • 打赏
  • 举报
回复
sorry,刚刚找到资料,发现确实是对的。:)
CDSoftwareWj 2002-04-15
  • 打赏
  • 举报
回复
苯,人家zx_wang(wzx) 给你金子你都不要,什么"这个的话就必须要填写一个Edit了"!!!好好看看吧
CDSoftwareWj 2002-04-15
  • 打赏
  • 举报
回复
to 1999() ( ) 信誉:100 2002-04-15 17:38:00 得分:0


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

我的意思是,判断edit的内容,添加where子句。
如果,所有的edit都为空,那么,最后的的是
'select * from table'

zx_wang(wzx) 所写的:
s:='select * from table where 1=1 ';
这个的话就必须要填写一个edit了,就是多了一个
’where 1=1 ‘出来了。并非是我要的。

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



加上一个 'where 1=1' 是个巧方法,不影响结果!!!!!!!!!!!!!!!!!! 还省去好多判断和字符串操作!!!!!!!!!!!!!
_i_ 2002-04-15
  • 打赏
  • 举报
回复
zx_wang(wzx) 同志是不是我的同事,为何和我的方法一模一样
blazingfire 2002-04-15
  • 打赏
  • 举报
回复
To:1999()
其实像zx_wang(wzx) 那样作是非常高明的,你不一定非要在所的Edit为空的时候就不要Where。
上面我写的程序中把所有的“If Edit.Text<>'' then”换成“If Trim(Edit1.Text)<>''”这样要好些
blazingfire 2002-04-15
  • 打赏
  • 举报
回复
你可以在一个窗体上放三个TEdit试一下。
blazingfire 2002-04-15
  • 打赏
  • 举报
回复
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;
icd 2002-04-15
  • 打赏
  • 举报
回复

if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
s:= s+' where ';
……

and 应该改成or吧
hwkknd 2002-04-15
  • 打赏
  • 举报
回复
理论正确,只是差了一个加不加where 的问题,那就在s:='select * from table'之后加一个判断是否所有Edit是否都为空的语句:
if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
s:= s+' where ';
……
……//追加文本框的内空
ponycsdn 2002-04-15
  • 打赏
  • 举报
回复
你用数组
这是我提出的问题,尽管美得到解决可是思路是对的。
http://www.csdn.net/expert/topic/648/648059.xml?temp=.1813928

给我加点分,我穷得可怜。
Billy_Chen28 2002-04-15
  • 打赏
  • 举报
回复
注意你生成的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 ';
......
1999 2002-04-15
  • 打赏
  • 举报
回复
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

我的意思是,判断edit的内容,添加where子句。
如果,所有的edit都为空,那么,最后的的是
'select * from table'

zx_wang(wzx) 所写的:
s:='select * from table where 1=1 ';
这个的话就必须要填写一个edit了,就是多了一个
’where 1=1 ‘出来了。并非是我要的。

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
StarRains 2002-04-15
  • 打赏
  • 举报
回复
照zx_wang(wzx)所说的思路做:

但要根据查询条件的字段类型判断加引号的个数、是否要重新格式化字串(例如时间)
sillyer 2002-04-15
  • 打赏
  • 举报
回复
照zx_wang(wzx)所说的做就对了。
icd 2002-04-15
  • 打赏
  • 举报
回复

A到1AX都是字符型的吗
把你生成的SQL语句放到SQL Explore里执行一下,看是什么错误
eagle123 2002-04-15
  • 打赏
  • 举报
回复
你要根据子段的类型来判断是否加引号
zx_wang 2002-04-15
  • 打赏
  • 举报
回复
begin
s:='select * from table where 1=1 ';
if edit1.text<>'' then
s:=s+' and A1='''+edit1.text+''''
else
……

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧