关于DBXPRESS中的TSQLQUERY使用日期参数的问题(100分)

  • 主题发起人 七年之痒
  • 开始时间

七年之痒

Unregistered / Unconfirmed
GUEST, unregistred user!
SQLQuery1->Close();
SQLQuery1->SQL->Clear();
SQLQuery1->SQL->Add("select gh,rq,oper,ptable,pkey from log_view where
         trunc(rq)>=:rq1 and trunc(rq)<=:rq2 and oper like :eek:per&quot;);
SQLQuery1->Params->ParamByName(&quot;rq1&quot;)->Value=DateTimePicker1->Date;
SQLQuery1->Params->ParamByName(&quot;rq2&quot;)->Value=DateTimePicker2->Date;
SQLQuery1->Open();
为什么会报错:DBX错误:Invalid Field Type 呢?我的RQ字段的数据类型是DATE没有错呀
 
SQLQuery1->Close();
SQLQuery1->SQL->Clear();
SQLQuery1->SQL->Add(&quot;select gh,rq,oper,ptable,pkey from log_view where
         rq>=:rq1 and rq<=:rq2 and oper like :eek:per&quot;);
SQLQuery1->Params->ParamByName(&quot;rq1&quot;)->AsDate=DateTimePicker1->Date;
SQLQuery1->Params->ParamByName(&quot;rq2&quot;)->AsDate=DateTimePicker2->Date;
SQLQuery1->Params->ParamByName(&quot;oper&quot;)->AsString=&quot;???????&quot;;
SQLQuery1->Open();
 
TRUNC(RQ)应该没问题,是ORACLE数据库,我用ADOQUERY这样做没有问题的 kaida 的方法我试了还是不行呀,如果把日期的参数去掉没问题只要加上
SQLQuery1->Params->ParamByName(&quot;rq1&quot;)->AsDate=DateTimePicker1->Date;
SQLQuery1->Params->ParamByName(&quot;rq2&quot;)->AsDate=DateTimePicker2->Date;
就报错:DBX错误:Invalid Field Type .是不是DBEXPRESS的问题呀?
 
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add(&quot;select gh,rq,oper,ptable,pkey from log_view where
         convert(char(10),rq,21)>=:rq1 and convert(char(10),rq,21)<=:rq2 and oper like :eek:per&quot;);
SQLQuery1.Params.ParamByName(&quot;rq1&quot;).value:=formatdatetime('yyyy-mm-dd',DateTimePicker1.Datetime);
SQLQuery1.Params.ParamByName(&quot;rq2&quot;).value:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Datetime);
SQLQuery1.Params.ParamByName(&quot;oper&quot;).value:=&quot;???????&quot;;
SQLQuery1.Open;
在查询语句中,条件限定的是日期而不是具体时间时是比较麻烦点,稍不注意就会出现查询的数据不是自己想要的东西,我一般都是用上述方法来查询。
 
gmshzw:谢谢你的帮助,不过,好象你说的一些函数是在SQLSERVER数据库下使用的吧?我连接的是ORACLE数据库,在SQLSERVER或者ORACLE里面用ADOQUERY连接是没问题的,但用DBEXPRESS连接就会出现这个问题.你的办法我试了还是不行,不过还是真诚的谢谢你的帮助
 
那你就直接在sql语句中直接赋值就行了嘛!
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add(&quot;select gh,rq,oper,ptable,pkey from log_view where
         convert(char(10),rq,21)>='''+formatdatetime('yyyy-mm-dd',DateTimePicker1.Datetime)
+''' and convert(char(10),rq,21)<='''+formatdatetime('yyyy-mm-dd',DateTimePicker1.Datetime)+''' and oper like :eek:per&quot;);
SQLQuery1.Open;
在sqlserver还是在oracle中,查询语句是差不多的,你可以用cast来转换,主要的问题是参数赋值的问题。
 
dbexpress挺烦人, BUG多, 打了补丁也不行.
我认为楼主的问题是dbExpress的BUG, 它把DateTime类型的字段映射为TSQLTimeStampField字段. 所以你可以设置参数的DataType为ftDouble, 再赋值.
 
SQLQuery1->Close();
SQLQuery1->SQL->Clear();
SQLQuery1->Params->Clear();
SQLQuery1->SQL->Add(&quot;select gh,rq,oper,ptable,pkey from log_view where gh like :gh and trunc(rq)>=to_date:)rq1) and trunc(rq)<=to_date:)rq2) and oper like :eek:per&quot;);
SQLQuery1->Params->CreateParam(ftString,'gh',ptInput);
SQLQuery1->Params->CreateParam(ftString,'oper',ptInput);
SQLQuery1->Params->CreateParam(ftFloat,'rq1',ptInput);
SQLQuery1->Params->CreateParam(ftFloat,'rq2',ptInput);
SQLQuery1->Params->ParamByName(&quot;gh&quot;)->Value=&quot;%&quot;+gh1+&quot;%&quot;;
SQLQuery1->Params->ParamByName(&quot;oper&quot;)->Value=&quot;%&quot;+oper1+&quot;%&quot;;
SQLQuery1->Params->ParamByName(&quot;rq1&quot;)->Value=DateTimePicker1->Date;
SQLQuery1->Params->ParamByName(&quot;rq2&quot;)->Value=DateTimePicker2->Date;
SQLQuery1->Open();
这样做依然不可以,还是报错:DBX错误:Invalid Field Type
 
顶部