简体   繁体   中英

Delphi 10.1 FireDac and Sqlite date issue

I have a table that is created in my android app.

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

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. Which tells me that the Date(STime) dosent work in Delphi.

Have also tried strfTime(%Y-&M-%D, STime) as SDate

Delphi dosent seem to like strfTime android device crashes.

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.

Why not in my app.

Stop concatenating your SQL, and use parameters instead. 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.

Something like this should work (untested, because I don't have SQLite on this machine):

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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