Recent

Author Topic: SQLite3 Date Problem  (Read 4938 times)

pat03uk

  • New Member
  • *
  • Posts: 20
SQLite3 Date Problem
« on: November 18, 2024, 03:06:18 pm »
I want to write a date to an SQLite3 database table, then retrieve it to a DBGrid column.

I enter the date eg 05/12/2022 as the string 2022-12-05 into Text Field

Using DBBrowser If I enter:
   SELECT  strftime('%d/%m/%Y', xDate), xNumber from transactions, I get the right result ie 05/12/2022.

If I run the query:
   SQLQuery.SQL.Text(SELECT  strftime(''%d/%m/%Y'', xDate), xNumber from transactions)
   DataSource1.DataSet := SQLQuery1;
   DBGrid1.DataSource := DataSource1;
   SQLQuery1.Open;

The date column of the grid contains all blanks.

Why should this be? Am I doing everything correctly?

Pat

Zvoni

  • Hero Member
  • *****
  • Posts: 2800
Re: SQLite3 Date Problem
« Reply #1 on: November 18, 2024, 03:30:20 pm »
If DD/MM/YYYY is your local format, then don't use strftime, but use Date-Function

Code: SQL  [Select][+][-]
  1. SELECT  DATE(xDate) AS xDate, xNumber FROM transactions
"strftime" returns a String/Text,
"Date(x)" returns a Date
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

pat03uk

  • New Member
  • *
  • Posts: 20
Re: SQLite3 Date Problem
« Reply #2 on: November 18, 2024, 03:55:12 pm »
Thanks, tried:
    SELECT  DATE(xDate) AS xDate, xNumber FROM transactions

this results in dates as 2022-12-05. (I want 05/12/2022)

If I try:

    SELECT  xDate, xNumber FROM transactions

it results in the same 2022-12-05

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: SQLite3 Date Problem
« Reply #3 on: November 18, 2024, 04:58:58 pm »
You need to define what format you want the column to display directly on the DBGrid
Code: Pascal  [Select][+][-]
  1. uses
  2.   db;
  3.  
  4. var
  5.   i: Integer;
  6. begin
  7.   SQLQuery.SQL.Text := 'SELECT xDate, xNumber from transactions';
  8.   DataSource1.DataSet := SQLQuery1;
  9.   DBGrid1.DataSource := DataSource1;
  10.   SQLQuery1.Open;
  11.   for i := 0 to DBGrid1.Columns.Count - 1 do
  12.   begin
  13.     if DBGrid1.Columns[i].Field.DataType = ftDate then
  14.       DBGrid1.Columns[i].DisplayFormat := 'dd/mm/yyyy';
  15.   end;
  16.   //...
  17.  
Best regards / Pozdrawiam
paweld

pat03uk

  • New Member
  • *
  • Posts: 20
Re: SQLite3 Date Problem
« Reply #4 on: November 18, 2024, 05:44:48 pm »
Thanks, tried that, no difference.

Bit puzzled, how do I set Field.DataType to ftDate.
DisplayFormat is already set to dd/mm/yyyy in the Object Inspector

Pat

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: SQLite3 Date Problem
« Reply #5 on: November 18, 2024, 06:43:51 pm »
I am not sure it will help or not. I just want to share how I handle date time data in SQLite.

Code: Pascal  [Select][+][-]
  1. const
  2.   DateTimeFormat = 'dd-mm-yy hh:nn:ss';
  3.  
  4. function TimeFormatted(T: TDateTime): string;
  5. begin
  6.   Result := FormatDateTime(DateTimeFormat, T);
  7. end;

The data time data will be stored as REAL data type in SQLite. And each time when showing it to user, I always call TimeFormatted. Because I consistently use DateTimeFormat, my code works. And I prefer to use TStringGrid, because I can format and show the data in whatever way I like.

Note:
I guess you already know, TDateTime actually is Double in Pascal.

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: SQLite3 Date Problem
« Reply #6 on: November 18, 2024, 06:46:36 pm »
Actually, I forgot that “/” is a replacement for the date separator. You need to take the “/” in quotation marks.
Sample in attachment.
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 16420
  • Censorship about opinions does not belong here.
Re: SQLite3 Date Problem
« Reply #7 on: November 18, 2024, 07:04:20 pm »
Handoko provided a better example: store it as TDatetime and convert as necessary.
Because the conversion code has the correct TFormatsettings for the language.
There is nothing wrong with being blunt. At a minimum it is also honest.

pat03uk

  • New Member
  • *
  • Posts: 20
Re: SQLite3 Date Problem
« Reply #8 on: November 18, 2024, 07:44:35 pm »
I’m storing the date as a string.
My understanding of SQLite is that you can store as string, integer or real - how do you store as TDate or TDatetime?
Pat

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: SQLite3 Date Problem
« Reply #9 on: November 18, 2024, 07:46:25 pm »
Didn't I already say, TDateTime actually is Double in Pascal?

Storing date data as string (in SQLite) may work too, but you need to extra carefully consistently format the data correctly. Extra work, not worth in my opinion.
« Last Edit: November 18, 2024, 07:50:28 pm by Handoko »

rvk

  • Hero Member
  • *****
  • Posts: 6643
Re: SQLite3 Date Problem
« Reply #10 on: November 18, 2024, 07:47:20 pm »
I’m storing the date as a string.
My understanding of SQLite is that you can store as string, integer or real - how do you store as TDate or TDatetime?
Pat
Yes, but how did you store it?
What real value (string) holds that field?
If it's not recognized as a date, it can't translate it.

rvk

  • Hero Member
  • *****
  • Posts: 6643
Re: SQLite3 Date Problem
« Reply #11 on: November 18, 2024, 07:49:44 pm »
For example

Quote
SQLite does not have a dedicated date/time datatype. Instead, date and time values can stored as any of the following:

ISO-8601   A text string that is an ISO 8601 date/time value. Example: '2025-05-29 14:16:00'
Julian day number   The number of days including fractional days since -4713-11-24 12:00:00 Example: 2460825.09444444
Unix timestamp   The number of seconds including fractional seconds since 1970-01-01 00:00:00 Example: 1748528160
https://www.sqlite.org/lang_datefunc.html

So if you saved it as 05/12/2024 then it's not a valid date.
You need to save it as 2024-12-05 as string.

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: SQLite3 Date Problem
« Reply #12 on: November 18, 2024, 07:52:20 pm »
rvk showed the example, why I said you need to extra carefully consistently format the data correctly.

pat03uk

  • New Member
  • *
  • Posts: 20
Re: SQLite3 Date Problem
« Reply #13 on: November 18, 2024, 07:59:38 pm »
I’m saving the date as a string as 2022-12-05 (see first post)

Pat

rvk

  • Hero Member
  • *****
  • Posts: 6643
Re: SQLite3 Date Problem
« Reply #14 on: November 18, 2024, 08:06:38 pm »
Bit puzzled, how do I set Field.DataType to ftDate.
DisplayFormat is already set to dd/mm/yyyy in the Object Inspector
In object inspector?
Did you precreated the fields?
That often goes horribly wrong.

It's best you let fpc create those fields automatically.
Not sure if that's the problem here.

Otherwise you need to create a small example which reproduces the problem.

 

TinyPortal © 2005-2018