var str:string;
begin
str:='select * from stu where';
if edit1.Text<>'' then
begin
str:=str+'s_id like '''+edit1.Text+'''';
end;
if edit2.Text <>'' then
begin
str:=str+'and s_name like '''+edit2.Text+'''';
end; adoquery1.Close ;
adoquery1.SQL.Clear ;
// adoquery1.SQL.Add('select * from stu where s_id like '''+edit1.Text+'''');
adoquery1.SQL.Add(str);
adoquery1.Open ;
提示from子句出现错误,晕,这个错在哪里啊
begin
str:='select * from stu where';
if edit1.Text<>'' then
begin
str:=str+'s_id like '''+edit1.Text+'''';
end;
if edit2.Text <>'' then
begin
str:=str+'and s_name like '''+edit2.Text+'''';
end; adoquery1.Close ;
adoquery1.SQL.Clear ;
// adoquery1.SQL.Add('select * from stu where s_id like '''+edit1.Text+'''');
adoquery1.SQL.Add(str);
adoquery1.Open ;
提示from子句出现错误,晕,这个错在哪里啊
begin
str:='select * from stu ';
if edit1.Text<>'' then
begin
str:=str+'where s_id like '''+edit1.Text+'''';
end;
if edit2.Text <>'' then
begin
str:=str+'and s_name like '''+edit2.Text+'''';
end; adoquery1.Close ;
adoquery1.SQL.Clear ;
// adoquery1.SQL.Add('select * from stu where s_id like '''+edit1.Text+'''');
adoquery1.SQL.Add(str);
adoquery1.Open ;
begin
str:='select * from stu';
if edit1.Text<>'' then
begin
str:=str+' where s_id like '''+edit1.Text+'''';
Tmp:=true;
end
else Tmp:=false;
if edit2.Text <>'' then
begin
if Tmp then str:=str+' and'
else str:=str+' where';
str:=str+' s_name like '''+edit2.Text+'''';
end; adoquery1.Close ;
adoquery1.SQL.Clear ;
// adoquery1.SQL.Add('select * from stu where s_id like '''+edit1.Text+'''');
adoquery1.SQL.Add(str);
adoquery1.Open ;
这个问题,可以用if来解决的,现在问题是两个edit都不是空的也会出现问题,我发现delphi中的"""号好象很复杂,
adoquery1.SQL.Clear ;adoquery1.SQL.Add('select * from stu');
if edit1.Text <>'' then
begin
adoquery1.SQL.Add('where S_ID like '''+edit1.Text+'''');
end;
if edit2.Text <>'' then
begin
adoquery1.SQL.Add('and S_NAME like '''+edit2.text+'''');
end
else begin
adoquery1.SQL.Add('where S_NAME like '''+edit2.Text+'''');
end;
adoquery1.Open ;
我是这样控制的,可是要出错,5555555,高手门指点下啊
但edit为空,edit2不空的时候就变成
select * from stu where and s_name like ...了
所以还是用str='select * from stu where 1=1'然后在if条件成立时加and语句
begin
str:='';
if edit1.Text<>'' then
begin
str:=str+'and where s_id like '''+edit1.Text+'''';
end;
if edit2.Text <>'' then
begin
str:=str+'and s_name like '''+edit2.Text+'''';
end; adoquery1.Close ;
adoquery1.SQL.Clear ;
adoquery1.sql.text:='select * from stu where 1=1'+str;
adoquery1.Open ;
指出你的 一点错误,用like 的时候,少不了%号