读书人

关于模糊查询程序的有关问题大大

发布时间: 2012-02-24 16:30:38 作者: rapoo

关于模糊查询程序的问题,请教各位大大啊
本人是一初学者,希望各位大大指教一下。感激不尽啊!

我尝试 写一个模糊查询的程序,pas文件源码附在后面了。其中renyuan.db是我在Delphi自带数据库DBDEMOS中建的表,其中有 bianhao xingming xingbie nianling shengao tizhong这6个属性列

窗体界面如下 http://tu.6.cn/pic/show-new/id/7636570/


http://tu.6.cn/pic/show-new/id/7636599/

但运行时总是出错。
提示:invalid use of keyword.Token: ,power(1+power(nianling-20)/5,2,-0.5) Line Number:1
按照提示 应该问题在 procedure TForm1.Button1Click(Sender: TObject) 中的
str1:=’power(1+power((nianling-'+floattostr(a)+')/5,2),-'+floattostr(m1)+')'; 这里的,但是我一直想不通到底错在什么地方,为什么错了。



unit cha414;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables,Math;

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
ComboBox5: TComboBox;
ComboBox6: TComboBox;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
Label4: TLabel;
DataSource1: TDataSource;
Query1: TQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
str,str0,str1,str2,str3:string;
a,m1,m2,m3,h,w:real;
begin
if combobox1.text='有点' then
m1:=0.5
else
m1:=1.25;
if combobox3.text='稍微有点' then
m2:=0.25
else
m2:=2;
if combobox5.Text='比较' then
m3:=0.75
else
m3:=4;
if combobox2.text='年轻' then
a:=20
else
a:=70;
if combobox4.text='高' then
h:=190
else
h:=160;
if combobox6.text='胖' then
w:=90
else if combobox6.text='中等' then
w:=60
else
w:=45;
str2:='power(1+power((shengao-'+floattostr(h)+')/10,2),-'+floattostr(m2)+')';
str1:='power(1+power((nianling-'+floattostr(a)+')/5,2),-'+floattostr(m1)+')';
str3:='power(1+power((tizhong-'+floattostr(w)+')/5,2),-'+floattostr(m3)+')';
str0:='create view shitu1 as select *,'+str1+' as age,'+str2+' as height,'+str3+' as weight from renyuan';
str:=str0;
query1.Close;
query1.SQL.Clear;
query1.SQL.Add(str);
query1.Active:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
str4:string;
begin
if radiobutton1.Checked=true then
str4:='select bianhao,xingming,xingbie,nianling,shengao,tizhong from shitu1 where age>=0.5 and height>=0.5 and weight>=0.5 and xingbie=''男''';
query1.Close;
query1.SQL.Clear;
query1.SQL.Add(str4);
query1.Active:=true;
if radiobutton2.Checked=true then
str4:='select bianhao,xingming,xingbie,nianling,shengao,tizhong from shitu1 where age>=0.5 and height>=0.5 and weight>=0.5 and xingbie=''女''';


query1.Close;
query1.SQL.Clear;
query1.SQL.Add(str4);
query1.Active:=true;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
query1.Close;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
query1.open;
end;

end.

[解决办法]
delphi自带的DBDEMOS用的是paradox库,它功能非常有限,既不支持
create view 也没有 power函数,你的程序当然不能用
(不同数据库支持的SQL有区别的)
另外如果是没有返回数据集的SQL,应该改
query1.Active:=true;

query1.ExecSQL;

[解决办法]
power是delphi的函数。怎么能在sql里出现呢?

先把生成完整的sql输出来,看看是否正确
[解决办法]
paradox不可能支持,除非你换数据库,如它自带的interbase库(别名为IBLocal)
但前提是你机上要安装interbase或firebird数据库服务

读书人网 >.NET

热点推荐