sql查询出错怎么办?(10分)

C

cuicui

Unregistered / Unconfirmed
GUEST, unregistred user!
我在写一个查询语句,用下拉框选择查询方式,用 控件输入查询内容,可是一执行查询命令
老出错,不知错在哪?
原程序:
procedure Tcx_form.Button1Click(Sender: TObject);
var sqlstr:string;
begin
query1.close;
query1.sql.clear;
if combobox1.Text='按证号' then
sqlstr:='select *from zjb where zh='''+edit1.Text+'''';
//query1.Close;
//query1.SQL.Clear;
query1.SQL.Add(sqlstr);
query1.Open;
if combobox1.Text='按医疗机构名称' then
sqlstr:='select * from zjb where jg='''+edit1.text+'''';
//query1.close;
//query1.sql.clear;
query1.sql.add(sqlstr);
//showmessage(sqlstr);
query1.Open;
if combobox1.Text='按地址' then
sqlstr:='select * from zjb where dz='''+edit1.text+'''';
query1.close;
query1.sql.clear;
query1.sql.add(sqlstr);
query1.Open;
if combobox1.Text='按负责人' then
sqlstr:='select * from zjb where fzr='''+edit1.text+'''';
query1.close;
query1.sql.clear;
query1.sql.add(sqlstr);
query1.Open;
end;
 
query1.close;
query1.sql.clear;
if combobox1.Text='按证号' then
begin //这里最好加上这东东
sqlstr:='select *from zjb where zh='''+edit1.Text+'''';
//query1.Close;
//query1.SQL.Clear;
query1.SQL.Add(sqlstr);
query1.Open;
end; //其它也要这样肯定不会出错
 
if 下面怎麼沒有begin ....end;呀,你那樣寫好象不對呀
要麼你的
query1.close;
query1.sql.clear;
query1.sql.add(sqlstr);
query1.Open;
隻要一處有就可以了,隻是你的sqlstr生成時用
if就是了
 
procedure Tcx_form.Button1Click(Sender: TObject);
var sqlstr:string;
begin
query1.close;
query1.sql.clear;
if combobox1.Text='按证号' then
sqlstr:='select * from zjb where zh='''+edit1.Text+'''';
if combobox1.Text='按医疗机构名称' then
sqlstr:='select * from zjb where jg='''+edit1.text+'''';
if combobox1.Text='按地址' then
sqlstr:='select * from zjb where dz='''+edit1.text+'''';
if combobox1.Text='按负责人' then
sqlstr:='select * from zjb where fzr='''+edit1.text+'''';
query1.close;
query1.sql.clear;
query1.sql.add(sqlstr);
query1.Open;
end;
 
执行sql查询的地方写成一个过程
procedure setsSQL(sSQL:string;Query1:TQuery);
begin
with Query1 do
try
SQL.Text:= sSQL;
Open;
Except
ExecSQL;
end;
end;
 
楼上的两位都已说了,我没什么可说的了
 
好像你的格式不太对,这样些sqlstr:='select * from zjb where zh='+''''+edit1.Text+''''
sql就应该不会报错了
 
同意楼上的,这样sqlstr:='select * from zjb where zh='+#39+edit1.Text+#39试试
 
query1 先写成 'select * from zjb ',

procedure Tcx_form.Button1Click(Sender: TObject);
var sqlstr:string;
begin
query1.close;
if combobox1.Text='按证号' then
sqlstr:=' zh='+''''+edit1.Text+'''';
if combobox1.Text='按医疗机构名称' then
sqlstr:=' where jg='+''''+edit1.Text+'''';
if combobox1.Text='按地址' then
sqlstr:= where dz='+''''+edit1.Text+'''';
if combobox1.Text='按负责人' then
sqlstr:= where fzr='+''''+edit1.Text+'''';
query1.sql.add(sqlstr);
query1.Open;
end;

程序更精简。
 
顶部