关于综合查询(100分)

Q

qun

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm3.ComboBox3Change(Sender: TObject);
begin
if combobox3.ItemIndex=0 then
//选择数据表为student
begin
combobox1.Items.Clear;
combobox1.Items.Add('学号');
combobox1.Items.Add('姓名');
combobox1.Items.Add('年龄');
combobox1.Items.Add('性别');
combobox1.Items.Add('班级');
combobox1.Items.Add('系别');
combobox1.Items.Add('电话号码');
combobox1.Items.Add('借阅许可');
end;
if combobox3.ItemIndex=1 then
//选择数据表为book
begin
combobox1.Items.Clear;
combobox1.Items.Add('书号');
combobox1.Items.Add('书名');
combobox1.Items.Add('作者');
combobox1.Items.Add('出版社');
combobox1.Items.Add('购买时间');
combobox1.Items.Add('是否允许借阅');
combobox1.Items.Add('是否允许预约');
end;
if combobox3.ItemIndex=2 then
//选择数据表为record
begin
combobox1.Items.Clear;
combobox1.Items.Add('记录号');
combobox1.Items.Add('学号');
combobox1.Items.Add('书号');
combobox1.Items.Add('借阅时间');
combobox1.Items.Add('归还时间');
combobox1.Items.Add('是否过期');
combobox1.Items.Add('是否预约');
end;
if combobox3.ItemIndex=3 then
//选择数据表为user
begin
combobox1.Items.Clear;
combobox1.Items.Add('用户名');
combobox1.Items.Add('密码');
combobox1.Items.Add('权限');
end;


end;

procedure TForm3.BitBtn1Click(Sender: TObject);//重置
begin
combobox3.Text:='';
combobox1.Text:='';
combobox2.Text:='';
edit1.Text:='';
end;

procedure TForm3.BitBtn2Click(Sender: TObject);
//查询
begin
with query1do
begin
close;
sql.Clear;
sql.Add('select * from student where '+combobox1.text+''+combobox2.text+''+quotedstr(edit1.text));
open;
end;

end;

end.
这是我使用的综合查询模块
combobox2.text中是连接条件
我用这个语句
“sql.Add('select * from student where '+combobox1.text+''+combobox2.text+''+quotedstr(edit1.text));”
只能查到表student中数据类型为文本的字段,查其它字段时则显示信息”标准表达式中数据类型不匹配“
如果我把这个语句改为”“sql.Add('select * from student where '+combobox1.text+''+combobox2.text+''+edit1.text);"则只能查询数据类型为数字的字段,查询其它字段时,显示错误信息“参数‘(edit1.text)中输入的内容‘没有默认值“
我用的后台数据库为Access
这是怎么回事?怎么样才能所有的字段都可以查询得到
 
给你个事例你参考下就知道了!
if form2.ComboBox1.Text='全部' then
ss:='' else
begin
if form2.ComboBox1.Text='分类' then
ss:='where 分类='+''''+form2.Edit5.Text+'''';
if form2.ComboBox1.Text='电话号码' then
ss:='where 电话号码='+''''+form2.Edit5.Text+'''';
if form2.ComboBox1.Text='地址' then
ss:='where 地址 like '+''''+'%'+form2.Edit5.Text+'%'+'''';
if form2.ComboBox1.Text='姓名' then
ss:='where 姓名 like '+''''+'%'+form2.Edit5.Text+'%'+'''';
if form2.ComboBox1.Text='单位' then
ss:='where 单位 like '+''''+'%'+form2.Edit5.Text+'%'+'''';
if form2.ComboBox1.Text='部门或职务' then
ss:='where 部门 like '+''''+'%'+form2.Edit5.Text+'%'+'''';
end;
with unit6.DataModule6.zsqlfriend do
begin
close;
sql.Clear;
sql.Add('SELECT * from friend');
sql.Add(ss);
open;
end;
能支持模糊查找的具体事例
另外数据库中字段设计为"不能为空字符串"时则需要判断你的编辑和查询条件
 
樓主如果要查詢三個不同表的話,可以跟據用戶的選擇來進行
if combobox3.ItemIndex=0 then
//选择数据表为student
with ADOQuery1do
begin
close;
sql.Clear;
sql.Add('select * from student where 學號 is not null');
if edit1.Text<>'' then
sql.Add('and 姓名='''+trim(edit1.Text)+'''');
open;
end;
if combobox3.ItemIndex=1 then
//选择数据表为book
with ADOQuery1do
begin
close;
sql.Clear;
sql.Add('select * from book where 學號 is not null');
if edit1.Text<>'' then
sql.Add('and 姓名='''+trim(edit1.Text)+'''');
open;
end;
if combobox3.ItemIndex=3 then
//选择数据表为user
with ADOQuery1do
begin
close;
sql.Clear;
sql.Add('select * from user where 學號 is not null');
if edit1.Text<>'' then
sql.Add('and 姓名='''+trim(edit1.Text)+'''');
open;
end;
查詢表裡具體是哪個字段可以自己改一下
 
sql.Add('select * from student where '+combobox1.text+' '+combobox2.text+' '+quotedstr(edit1.text));
字段名或邏輯符要有空格位子
edit1.text要根據字段的類型區分是否加quotedstr()函數。
 
各个表中的各个字段类型不同,不能用一种方式来设置条件!
比如:quotedstr(edit1.text)),如果用户查的数字,加了单引号就编程字符串了,数字字段如何和字符串比较?
 
我曾經寫過類似的功能,有幾點建議供參考
1.不要以combobox1.Items.Add('学号');的方式來加,應用
for i:=0 to table1.fieldcountdo
combobox1.Items.Add(table1.field.displaylabel)
的形式,結合tag,看哪些要加,哪些不要加
2.在組成sql語句時,要判斷所選字段的類型,這樣根據不同的字段類型來組織sql 語句
 
To 漂流的云
你这个方法确定不错,但是查一个字段的记录就要写一个语句,而我是想查一个表中的所有字段,并且希望只写一个查询语句
 
To qis2000
我用这样的方法怎么添加不进去呀,也就在combobox3中选择了student,在combobox1中什么东西也没有,这是怎么回事啊
 
procedure TForm3.ComboBox3Change(Sender: TObject);
var
i:integer;
begin
if combobox3.ItemIndex=0 then
begin
with query1do
begin
close;
sql.Clear;
sql.Add('select * from student');
open;
for i:=0 to query1.FieldCountdo
begin
combobox1.Items.Add(query1.Fields.DisplayLabel);
end;
end;
end;
当我在选择student时,直接显示student表中的所有记录,并且跳出错误信息“list index out of bounds(8)“,这是怎么回事?但combobox1中可以显示student表中的所有字段
 
procedure TForm3.ComboBox3Change(Sender: TObject);
var
i:integer;
begin
if combobox3.ItemIndex=0 then
begin
with query1do
begin
close;
sql.Clear;
sql.Add('select * from student');
open;
query1.first;
while not eofdo
combobox1.Items.Add(query1.Fields.DisplayLabel);//这个i你是怎么定义的?要改下了!
end;
end;
end;
 
你这个我做过,只能用带参数的查询方法了。直接拼字符串是不行的。
要像这样操作
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from employee where '+fieldname+tj+':F1');
//tj为条件
adoquery1.Parameters.ParamByName('F1').Value :=edit1.Text;
adoquery1.Open ;
这样才能不受字段类型的限制。
 
顺便给你个技巧:
//*** 添加中文字段到下拉框中*****
with combobox1do
begin
items.begin
Update;
items.clear;
for i:=0 to DBgrideh1.FieldCount-1do
begin
items.add(DBGridEh1.Columns.Title.Caption);
end;
items.EndUpdate ;
itemindex:=0;
end;
 
主要是因为用参数查询的时候数值参数,字符串参数,日期参数它们使用的方法不同
如:字符串要用 '''+参数+'''
日期要用 '+ 参数 +' 还要看是什么数据库
数值参数 '+ 参数 +'
所以你要相应的进行一下判断
 
我还是很不明白,谁能告诉我combobox3是选择的表,而combobox1则是在combobox3所选表所对应的字段,如何在combobox3中选择了表后在combobox1中生成其字段,上面的方法我都试过了,但只要一在combobox3中选择表,就已经自动查询了。我要的是在combobox3中选择了表,然后在combobox1中选择其字段,又在combobox2中选择条件,最后在edit1中添加字段值,最后才能查询。谁能帮我实现这个功能。
 
qun:
1.表面全部放在combobox3
2.根据选取的表名,定位一个query,把combobox1清空,把query中的字段的displaytext加入
3.combobox2中放入=,>等操作符
4。在edit1中输入要查询的值
按查询后,先判数combobox1 字段的类型,再具体写以下查询语句
'select * from '+combobox3.text+' where '+ combobox1.text +combobox2.text+'''+edit1.text+'''
这个应不难啊,不知在实现时问题在哪里?
 
教你一个小方法,看完了再回来试试:
var ss:string;
begin
ss:='select * from '+combobox3.text+' where '+ combobox1.text +combobox2.text+'''+edit1.text+'''
showmessage(ss);//看看你的SQL语句和语法。。。。。。
end;
//看完把ss的结果贴上来吧,记住,程序的结果不是只给客户看的,自己心中有结果才。。。。。
//最后成功要放分。。。。。。。。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
599
SUNSTONE的Delphi笔记
S
S
回复
0
查看
588
SUNSTONE的Delphi笔记
S
S
回复
0
查看
949
SUNSTONE的Delphi笔记
S
S
回复
0
查看
770
SUNSTONE的Delphi笔记
S
顶部