I have a SQLite
database with a table where one column is a type of DATETIME
. Te format of DATETIME
in SQLite
table is yyyy-mm-dd hh:nn:ss
, the format of DATETIME
in the pc is mm/dd/yyyy hh:nn:ss ampm
. When i write date to the database, then it is automatially converted to the format yyyy-mm-dd hh:nn:ss
. But when i do try to read it from the table, then i get the econverter
error, which says that yyyy-mm-dd hh:nn:ss
is not a correct format. My query
looks like that:
sqlQuery.SQL.Text := 'SELECT MAX(Pradzia) FROM Pamainos';
sqlQuery.Open();
ShiftPradzia := sqlQuery.Fields[0].AsDateTime; <-- I do get error here
sqlQuery.Close;
If i change Query
like that, then i do not get any error:
sqlQuery.SQL.Text := 'SELECT Pradzia FROM Pamainos WHERE ID = :_ID';
sqlQuery.Params.ParamByName('_ID').Value := fShiftID;
sqlQuery.Open();
ShiftPradzia := sqlQuery.Fields[0].AsDateTime;
sqlQuery.Close;
EDIT: Exact error message:
Project ProjectName.exe raised exception class EConverterError with message "2020-12-19 13:34:24.743" is not a valid date and time.
EDIT2: Works this way as well:
sqlQuery.SQL.Text := 'SELECT Pradzia FROM Pamainos WHERE Pradzia = (SELECT MAX(Pradzia) FROM Pamainos)';
From Using SQLite with FireDAC
For an expression in a SELECT list, SQLite avoids type name information. When the result set is not empty, FireDAC uses the value data types from the first record. When empty, FireDAC describes those columns as dtWideString. To explicitly specify the column data type, append :: to the column alias
Therefore:
sqlQuery.SQL.Text := 'SELECT MAX(Pradzia) as "MaxPradzia::datetime" FROM Pamainos';
Also you must ensure FDConnection.Params.Values['DateTimeFormat'] := 'string';
This is the default setting.