我想做一个数据库的动态查询---可以选择表中的一个或者几个字段,需要用
adoconenction,aodquery来实现。
例如:假设都是字符型,
如果checkbox1被选中则字段kind='''+Edit1.text+'''
checkbox2被选中则字段material='''+Edit2.text+'''
checkbox3被选中则字段type='''+Edit3.text+'''
sqltext:=kind='''+Edit1.text+''' and material='''+Edit2.text+''' and type='''+Edit3.text+''' 几个条件必须连接起来,组合查询。
我只试了一个条件的查询,sql语句没有错,但是查询不出结果,一直找不出问题所在!
请各位大虾赐教!解决立刻结贴。
adoconenction,aodquery来实现。
例如:假设都是字符型,
如果checkbox1被选中则字段kind='''+Edit1.text+'''
checkbox2被选中则字段material='''+Edit2.text+'''
checkbox3被选中则字段type='''+Edit3.text+'''
sqltext:=kind='''+Edit1.text+''' and material='''+Edit2.text+''' and type='''+Edit3.text+''' 几个条件必须连接起来,组合查询。
我只试了一个条件的查询,sql语句没有错,但是查询不出结果,一直找不出问题所在!
请各位大虾赐教!解决立刻结贴。
adoquery1.close;
adoquery1.sql.clear;
if checkbox1.checked then
adoquery1.sql.add('select * from table where kind='''+Edit1.text+'''');
if checkbox2.checked then
adoquery1.sql.add('select * from table where material='''+Edit2.text+'''');
if checkbox3.checked then
adoquery1.sql.add('select * from table where type='''+Edit3.text+''' ');
adoquery.open ;
adoquery.close;
既只要选择某个或者是某几个字段,输入值以后就可以查询
例如:如果我只要用一个字段查询checkbox1.checked
sql.text:='select * from table where type='''+Edit1.text+''''
假如使用两个字段查询(checkbox1.checked and checkbox2.checked):
sql.text:='select * from table where type='''+Edit1.text+''' and kind='''+edit2.text+''' '
多条件依次类推,我现在想要的就是这条语句,既可以通过checkbox选择需要的字段(通过编辑框对子段赋值),动态生成一条sql语句,用这一条语句去查询。 close;
adoquery1.sql.clear;
adoquery1.sql.text:=sqlstr;
///需要的就是怎样生成这一条语句(sqlstr),而且能查询出结果(我做的语句已经生成正确了,但是查询不出结果,实际上记录存在。)
adoquery1.prepared;
adoquery1.open;
close;
if checkbo1.checked then sql.add('select * from tablename where kind='''''edit1.text+'''');
if checkbox2.checked then sql.add('select * from tablename where material='''''edit2.text+'''');
if checkbo3.checked then sql.add('select * from tablename where type='''''edit3.text+'''');
open;
var
aSQL: string
i: integer;
hasCondition: boolean;
begin
hasCondition := false;
aSQL := 'SELECT * FROM TABLENAME';
for i := 0 to CheckBox.Items.Count - 1 do
begin
if CheckBox.Checked[i] then
begin
if not hasCondition then
begin
aSQL := aSQL + ' WHERE ';
hasCondition := true;
end;
aSQL := aSQL + CheckBox.Items[i] + ' = ' + TEdit(Panel1.Control[i]).Text
if i <> CheckBox.Items.Count - 1 then
aSQL := aSQL + ' AND ';
end;
end; with AdoQuery1 do
begin
Sql.clear;
Sql.Add(aSQL);
try
Open;
except
//handle your exception
end;
end;
end;
adoquery1.sql.clear;
adoquery1.sql.add('select * from table where 1=1')
if checkbox1.checked then
adoquery1.sql.add('and kind='''+Edit1.text+'''');
if checkbox2.checked then
adoquery1.sql.add('and material='''+Edit2.text+'''');
if checkbox3.checked then
adoquery1.sql.add('and type='''+Edit3.text+''' ');
adoquery.open ;
adoquery.close;
var
Str:string;
Str := '';
if 条件1 then
begin
if Str<>'' then
Str := Str+' and ';
Str := 条件1语句;
end;
if 条件2 then
begin
if Str<>'' then
Str := Str+' and ';
Str := 条件2语句;
end;
if 条件3 then
begin
if Str<>'' then
Str := Str+' and ';
Str := 条件3语句;
end;
........... SQL := ....+Str+....;
比如如果checkbox1的onclick事件里,如果checked:=true,设一标志Ifcheck1:=true;
判断 checkbox2,checkbox3...的标志IfCheck2,IfCheck3....是否为true
如果是的话sql.text:=sql.text+' and kind='''+Edit1.text+'''';
如果为否的话:sql.text:=sql.text+' where kind='''+Edit1.text+'''';
当查询出结果后置所有标志为false;
同理checkbox2.........标志位要为全局变量