小M我想要在程序里实现模糊查询可是总是不行,请各位大侠帮帮改错~谢谢了~
我的数据库是Access的~
procedure TBookQuery.Button1Click(Sender: TObject);
var
Content : string;
begin
try
ADOQuery1.Close;
begin
Content:=Edit1.Text;
if CheckBox1.Checked then
Content := '%'+Content +'%';
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from 图书信息表 where 书名 like :书名');
ADOQuery1.Parameters.ParamByName('书名').value := Content;
ADOQuery1.Open;
label4.Caption:= '共'+IntToStr(ADOQuery1.RecordCount)+'条记录';
if ADOquery1.RecordCount=0 then
showmessage('该书名所有的书不存在!')
end;
except
MessageDlg('查询失败',mtError,[mbok],0);
end;
end;
我的数据库是Access的~
procedure TBookQuery.Button1Click(Sender: TObject);
var
Content : string;
begin
try
ADOQuery1.Close;
begin
Content:=Edit1.Text;
if CheckBox1.Checked then
Content := '%'+Content +'%';
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from 图书信息表 where 书名 like :书名');
ADOQuery1.Parameters.ParamByName('书名').value := Content;
ADOQuery1.Open;
label4.Caption:= '共'+IntToStr(ADOQuery1.RecordCount)+'条记录';
if ADOquery1.RecordCount=0 then
showmessage('该书名所有的书不存在!')
end;
except
MessageDlg('查询失败',mtError,[mbok],0);
end;
end;
试试
如果只取记录直接
SELECT COUNT(*) FROM table要比你那么写效率要高得多
//干嘛不这样写procedure Tform.button1click(Sender:TObject);
var
str:string;
begin
try
str:='select * from 图书信息表 where 书名 like ''%'+edit1.text+'%'' ';
with adoquery do
begin
close;
sql.clear;
sql.add(str);
open;
lebel4.caption:='共' + inttostr(recordcount) +'条记录';
end;
except
showmessage('查询失败!');
end;
end;
回复fa_ge:非常感谢您的回答,确实可以实现模糊查询,但是我想要的是当 if CheckBox1.Checked then 执行模糊查询,我知道这对您来说非常简单,可是我改来改去都改不出满意的程序,还是想请您帮帮我~谢谢了~~~~~~~~
改动这个对你来说也不应该困难吧?{借用 鶴嘯九天 的代码:}
procedure TBookQuery.Button1Click(Sender: TObject);
var
str: string;
begin
if CheckBox1.Checked then
try
str:='select * from 图书信息表 where 书名 like ''%'+edit1.text+'%'' ';
with adoquery1 do
begin
close;
sql.clear;
sql.add(str);
open;
lebel4.caption:='共' + inttostr(recordcount) +'条记录';
end;
except
showmessage('查询失败!');
end;
end;
另外,如果你仅仅是要想知道指定条件的记录是否存在的话,用hsmserver的方法就不错。
不但不能执行模糊查询,连普通查询功能都失效了......
我真的很不明白hsmserver 说的意思......
不知道该怎么办......
我知道那不难,但是我怎么写都不成功,我以为我的逻辑已经对了,可是就是不行,
一定有什么细节是我所不懂的,所以才来请教~
请各位帮帮忙~~~~~~~~~
if checkbox1.checked then
beign
end;
这样就行吧
var
str: string;
begin
if CheckBox1.Checked then
begin
try
str:='select * from 图书信息表 where 书名 like ''%'+edit1.text+'%'' ';
with adoquery1 do
begin
close;
sql.clear;
sql.add(str);
open;
lebel4.caption:='共' + inttostr(recordcount) +'条记录';
end;
except
showmessage('查询失败!');
end;
end;
end;
我不知道该怎么放断点....你可以教我吗?
这个问题真的很棘手,各位大侠都没有办法帮我解决吗???????????
大家这样写SQL语句,不必书名当中出现:“%”或“'”之类的吗?
我以前也像大家的这样写,不过,后来测试部找出了这个的BUG问题太多,
所以换了一种写法。
oracle 数据库应该都可以的.
SQL.Clear;
begin
if QueryCBox.Checked then
begin
if QueryRBox1.Checked then
begin
SQL.Add('SELECT * FROM 商品库存 WHERE 拼音简码 LIKE :a');
Parameters.ParamByName('a').Value := QueryEdit.Text + '%';
end
else
begin
SQL.Add('SELECT * FROM 商品库存 WHERE 拼音简码 LIKE :a');
Parameters.ParamByName('a').Value :='%' + QueryEdit.Text + '%';
end;
end
else
begin
if QueryRBox1.Checked then
begin
SQL.Add('SELECT * FROM 商品库存 WHERE 商品名称 LIKE :a');
Parameters.ParamByName('a').Value := QueryEdit.Text + '%';
end
else
begin
SQL.Add('SELECT * FROM 商品库存 WHERE 商品名称 LIKE :a');
Parameters.ParamByName('a').Value :='%' + QueryEdit.Text + '%';
end;
end;
end;
Open;