简体   繁体   English

Delphi 10.1 FireDac和Sqlite日期问题

[英]Delphi 10.1 FireDac and Sqlite date issue

I have a table that is created in my android app. 我有一个在我的Android应用程序中创建的表。

CREATE TABLE IF NOT EXISTS `TimeSheet  
  `TKey` INTEGER PRIMARY KEY AUTOINCREMENT,
  `PriKey1` INT NOT NULL,
  `PriKey2` DEFAULT NULL,
  `PriKey3` DEFAULT NULL,
  `STime` DATETIME DEFAULT NULL,
  `ETime` DATETIME DEFAULT NULL,
  `Note` BLOB);

I have loaded some data using various dates. 我已经使用不同的日期加载了一些数据。

And I need to filter so as only one days data is shown at a time. 而且我需要过滤,以便一次只显示一天的数据。

Delphi code to do so is below 这样做的Delphi代码如下

FD := "a date";
QuTimeSheet.Active := False;
QuTimeSheet.SQL.Text := 'Select Date(STime) as SDate, Time(STime) as STime,     Time(ETime) as ETime, Task.TaskName From TimeSheet' + #13 + #10 +
'  LEFT JOIN task ON (TimeSheet.PriKey1 = task.TKey)';// + #13 + #10 +
'Where SDate = ''' + formatdatetime('YYYY-MM-DD',FD) + ''';';
QuTimeSheet.Active := True;

If I remove the where statement SDate has some DD-MM-1800 date. 如果删除where语句,SDate的日期为DD-MM-1800。 Which tells me that the Date(STime) dosent work in Delphi. 这告诉我Date(STime)剂量在Delphi中起作用。

Have also tried strfTime(%Y-&M-%D, STime) as SDate 还尝试了strfTime(%Y-&M-%D,STime)作为SDate

Delphi dosent seem to like strfTime android device crashes. 德尔福剂量似乎喜欢strfTime android设备崩溃。

Have tried using a filter 尝试使用过滤器

  QuTimeSheet.Filtered := False;
  QuTimeSheet.Filter := 'SDate = ''' + formatdatetime('YYYY-MM-DD',FD) + '''';
  QuTimeSheet.Filtered := True;

or 要么

  QuTimeSheet.Filtered := False;
  QuTimeSheet.Filter := 'SDate = ''' + Datetostr(FD) + '''';
  QuTimeSheet.Filtered := True;

This filters nothing. 这不会过滤任何内容。

This is such a simple where statement that works in MySql and on the Sqlite turorials site. 这是一个如此简单的where语句,可在MySql和Sqlite turorials网站上使用。

Why not in my app. 为什么不在我的应用中。

Stop concatenating your SQL, and use parameters instead. 停止连接您的SQL,并改用参数。 Also, stop converting things from one type to another yourself, and let the database driver do it for you; 另外,停止自己将事物从一种类型转换为另一种类型,并让数据库驱动程序为您完成; it knows how to quote things that need to be quoted (and not quote those that don't), and how to properly format date values. 它知道如何引用需要引用的内容(而不引用不需要的内容)以及如何正确格式化日期值。 It also prevents SQL injection. 它还可以防止SQL注入。

Something like this should work (untested, because I don't have SQLite on this machine): 这样的事情应该可以工作(未经测试,因为我在这台机器上没有SQLite):

var
  TheDate: TDateTime;
begin
  TheDate := EncodeDate(2015, 1, 1);
  QuTimeSheet.Active := False;
  QuTimeSheet.SQL.Text := 'Select Date(STime) as SDate, Time(STime) as STime,'#13 +
                        ' Time(ETime) as ETime, Task.TaskName From TimeSheet'#13 +
                        ' LEFT JOIN task ON (TimeSheet.PriKey1 = task.TKey)'#13 +
                        'Where SDate = :SDateVal';
  QuTimeSheet.ParamByName('SDateVal').AsDateTime := TheDate;
  QuTimeSheet.Active := True;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM