procedure TForm1.Button1Click(Sender: TObject); var selectstr,sql:string; //////////按空格分词 strlist:tstrings; position:integer; begin selectstr:=' 数据库 delphi 中国 '; strlist:=TStringList.create(); while pos(' ',selectstr)>0 do begin position:=pos(' ',selectstr); if position=1 then selectstr:=copy(selectstr,2,length(selectstr)-1) else begin strlist.Add(copy(selectstr,1,position)); selectstr:=copy(selectstr,position,length(selectstr)-position); showmessage(selectstr); end; end; //分词完成,词放在strlist中,下面为sql语句 sql:='select * from 表名 where (字段名 like %delphi%) or (字段名 like %数据库%') end;
procedure TForm1.Button1Click(Sender: TObject); var selectstr,sql:string; //////////按空格分词 strlist:tstrings; position,i:integer; begin selectstr:=' 数据库 delphi 中国 '; strlist:=TStringList.create(); while pos(' ',selectstr)>0 do begin position:=pos(' ',selectstr); if position=1 then selectstr:=copy(selectstr,2,length(selectstr)-1) else begin strlist.Add(copy(selectstr,1,position)); selectstr:=copy(selectstr,position,length(selectstr)-position); showmessage(selectstr); end; end; //分词完成,词放在strlist中,下面为sql语句 sql:='select * from 表名 where '; for i:=0 to strlist.Count-1 do if i=strlist.Count-1 then sql:=sql+'(字段名 like ''%'+strlist.Strings[i]+'%'')' else sql:=sql+'(字段名 like ''%'+strlist.Strings[i]+'%'') or'; sql:=sql+')'; showmessage(sql); end;
用substring可以吗?
var selectstr,sql:string; //////////按空格分词
strlist:tstrings;
position:integer;
begin
selectstr:=' 数据库 delphi 中国 ';
strlist:=TStringList.create();
while pos(' ',selectstr)>0 do
begin
position:=pos(' ',selectstr);
if position=1 then selectstr:=copy(selectstr,2,length(selectstr)-1)
else
begin
strlist.Add(copy(selectstr,1,position));
selectstr:=copy(selectstr,position,length(selectstr)-position);
showmessage(selectstr);
end;
end;
//分词完成,词放在strlist中,下面为sql语句
sql:='select * from 表名 where (字段名 like %delphi%) or (字段名 like %数据库%')
end;
var selectstr,sql:string; //////////按空格分词
strlist:tstrings;
position,i:integer;
begin
selectstr:=' 数据库 delphi 中国 ';
strlist:=TStringList.create();
while pos(' ',selectstr)>0 do
begin
position:=pos(' ',selectstr);
if position=1 then selectstr:=copy(selectstr,2,length(selectstr)-1)
else
begin
strlist.Add(copy(selectstr,1,position));
selectstr:=copy(selectstr,position,length(selectstr)-position);
showmessage(selectstr);
end;
end;
//分词完成,词放在strlist中,下面为sql语句 sql:='select * from 表名 where ';
for i:=0 to strlist.Count-1 do
if i=strlist.Count-1 then sql:=sql+'(字段名 like ''%'+strlist.Strings[i]+'%'')'
else sql:=sql+'(字段名 like ''%'+strlist.Strings[i]+'%'') or';
sql:=sql+')';
showmessage(sql);
end;
我出现了参数不足,期待为一的错误,
怎么改都不行