在ADO(ODBC)中使用datetime参数会丢失时间部分

时间:2011-12-30 11:01:36

标签: delphi ado delphi-xe

昨天,当我忙于使用SQLLite编写一些单元测试时,我偶然发现了这个问题。我的环境是Windows7 / Delphi XE。

将TADOQuery与TDateTime参数结合使用会导致时间部分丢失。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ADODb, DateUtils, DB;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

var DbConn : TADOConnection;
    Qry    : TADOQuery;
    DT     : TDateTime;

begin
 DBConn := TADOConnection.Create(nil);
 DBConn.ConnectionString := 'Provider=MSDASQL.1;Extended Properties="DRIVER=SQLite3 ODBC Driver;Database=:memory:;LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"';
//   DBConn.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=True;User ID=%0:s;Password=%1:s;Extended Properties="DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;PORT=3306;DATABASE=test;USER=root;PASSWORD=rrr;OPTION=1048579"';
 Qry := TADOQuery.Create(nil);
 Qry.Connection := DbConn;
 try
  DBConn.Connected := True;
  Qry.SQL.Text := 'CREATE TABLE test(d datetime)';
  Qry.ExecSQL;
  Qry.ParamCheck := True;
  Qry.SQL.Text := 'INSERT INTO test (d) VALUES (:d)';
  //Qry.Parameters.ParseSQL(Qry.SQL.Text, True); // not needed
  TryEncodeDateTime(1999, 12, 12, 10, 59, 12, 0, DT);
  Qry.Parameters.ParamByName('d').Value := DT;
  Qry.Parameters.ParamByName('d').DataType := ftDateTime;
  Qry.ExecSQL;
  Qry.SQL.Text := 'SELECT d FROM test';
  Qry.Open;
  ShowMessage(FormatDateTime('MM/DD/YYYY HH:NN:SS', Qry.FieldByName('d').AsDateTime));
 finally
  FreeAndNil(Qry);
  FreeAndNil(DbConn);
 end;
end;

有趣的是,当我评论该行Qry.Parameters.ParseSQL(Qry.SQL.Text, True);它会正常工作。我需要ParseSQL部分,因为我正在构建一个迷你ORM,因此它需要知道必须映射哪些参数。 一些观察:

  • 使用MySQL5进行相同的测试显示了同样的问题(无论ParseSQL部分如何)。
  • 此代码适用于SQL Server和OLEDB驱动程序。

我搜索了网,发现了一些有趣的链接:

http://tracker.firebirdsql.org/browse/ODBC-27

http://embarcadero.newsgroups.archived.at/public.delphi.database.ado/201107/1107112007.html

http://bugs.mysql.com/bug.php?id=15681

第一个链接建议'修复'ADODB.pas,这是我不想做的事情。 阅读最后一个链接,似乎ADO将日期时间值映射到日期。

答案我不想听:使用其他库/组件(如Dbexpress,Zeoslib,......)

我不确定解决这个问题的最明智的方法是什么。

Linas和Marjan Venema建议我可以省略ParseSQL部分。 因此代码现在可以使用SQLlite IF省略行Qry.Parameters.ParamByName('d').DataType := ftDateTime;

但MySQL拒绝节省时间部分。 我在这里看到了ADO和MySQL ODBC之间的兼容性问题吗?

2 个答案:

答案 0 :(得分:8)

我使用SQL Server对此进行了一些测试,并且在使用MSDASQL.1(ODBC)时遇到完全相同的问题。您的代码适用于SQLOLEDB.1和SQLNCLI10.1。

如果您将参数类型指定为ftString,它将使用ODBC保存(至少在SQL Server上)。

Qry.Parameters.ParamByName('d').DataType := ftString;
Qry.Parameters.ParamByName('d').Value := DateTimeToStr(DT);

注意:使用DateTimeToStr时要小心本地设置,它可能不会产生数据库想要的内容。安全的赌注是使用yyyy-mm-dd hh:mm:ss[.fff]

<强>更新

您也可以自己将ado参数的数据类型设置为adDBTimeStamp。当您使用adDate时,ADODB会将其设置为ftDateTime

Qry.Parameters.ParamByName('d').ParameterObject.Type_ := adDBTimeStamp;
Qry.Parameters.ParamByName('d').Value := DT;

答案 1 :(得分:0)

我遇到了与VFPOLEDB(Visual FoxPro驱动程序)相同的问题,但adDBTimeStamp的技巧并不起作用。 FWIW,这是VFPOLEDB 9.0.0.5815与Delphi 7以及RAD Studio 10(西雅图)。

在VFPOLEDB的情况下,还有另一种可能的解决方法,它基于以下事实:在Fox中,日期/时间值的基础表示(类型&#39; T&#39;)与日期值相同(类型&#39; D&#39;)由OLEDB传递,即64位双精度,其中整数部分表示自0001年1月1日以来的天数,小数部分表示一天中的时间。

如果表格字段的类型为&#39; T&#39;和OLEDB通过约会,然后福克斯通过什么都不做来强迫价值。所以这里所需要的只是在小数部分中添加。

示例,start_time是日期/时间字段:

cmd.CommandText := 'insert into foo (start_time) values (:start_time)';
cmd.Parameters.ParamByName('start_time') := Now;
cmd.Execute;

这会在表格中显示2016-05-22 00:00:00

现在,像这样添加小数部分:

cmd.CommandText := 'insert into foo (start_time) values (:start_time + :fraction)';
t := Now;
cmd.Parameters.ParamByName('start_time') := t;
cmd.Parameters.ParamByName('fraction') := Frac(t);
cmd.Execute;

这会给2016-05-22 02:17:42。即使它们没有显示,也会保留小数秒。

相关问题