Jump to content
new_x

Storing and Displaying DateTime Values in SQLite DB

Recommended Posts

I am using FireDAC to handle a simple DB Program,

I am using TFDConnection, TFDQuery and TDataSource and TDBGrid components to handle DB operations.

It is a simple visitor recording DB application,
visitor table is created as;

1- create table visitors (name nvarchar(30) not null COLLATE NOCASE,
lastname nvarchar(30) not null COLLATE NOCASE,
enterance_time integer not null,
exit_time integer not null);

2- for the time record insertion into the table I used,

DateTimeToUnix(Now()) values for the enterance_time and exit_time fields.

But I do not know how to show normal date time string (like mm/dd/yyyy hh:mm:nn) in the DBGrid correponding fields when I want to list the records of the table to show to the user. So if I restate the problem "how can we display the date and time values in "mm/dd/yyyy format if the datetime value is stored in the Sqlite db as INTEGER"

 

Regards,

Share this post


Link to post

Do you need to store exit_time value as INTEGER or would you prefer it stored in a format that you can read (like yyyy-mm-dd)?

Share this post


Link to post
6 hours ago, weirdo12 said:

Do you need to store exit_time value as INTEGER or would you prefer it stored in a format that you can read (like yyyy-mm-dd)?

I thought that I should store both enterance_time, exit_time values as Integer. Whenever a new record comes, I want to check if that new record's date time values falls into already stored records' date time values. This way it should be simple to compare date time values in Delphi am I right?

Share this post


Link to post
18 hours ago, Fr0sT.Brutal said:

Try with custom TField.OnGetText.

I am novice at using Delphi. How to assign that event? I am using FDConnection, FDQuery, DataSource and DBGrid, when querying the DB data, I am constructing SQL query statements (select * from ...)  based on user input, then executing that SQL statements with FDQuery showing the result in the DBGrid.

Which component does have that event?

Thanks 

Edited by new_x
More Explanation

Share this post


Link to post
5 hours ago, Hans J. Ellingsgaard said:

If you store the values as a Timestamp field, you can show it in the grid with entrance_time.AsDateTime

As I stated above I am novice at using Delphi, At which point in code, I should call  entrance_time.AsDateTime?

Share this post


Link to post


First declare fields
Then set event on GetText for the field and code the event

 

procedure TForm1.FDTable1Date_exitGetText(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
if Sender.IsNull then text:='n.d.'
                 else Text:=FormatDateTime('mm-dd-yyyy',UnixToDateTime(Sender.Value));
end;


image.thumb.png.a2fe993fedb2834cc34ddcdb081ff0c0.png

on design date_exit should be an integer but at run time "magic" happens
image.thumb.png.72055711581d297c7d295043937112d0.png

 

Now let speak about another method for Timestamp type (my date_create)
You just have to set property displayformat

image.png.223c492bd6a343aee1c1de9f0f62c94a.png
to get the date_create formatted at runtime but also at design time (yellow)

 

image.thumb.png.127c3299b19f8020c8ad1a8b6cbe74c1.png

Edited by Serge_G
  • Like 1

Share this post


Link to post
1 hour ago, Serge_G said:

You just have to set property displayformat

But for this the field must be of type TDateTimeField which AFAIU does not automatically happen for SQLite as the engine can't know whether an integer field is timestamp or just integer. So user has to create the field at runtime doing tricks with FieldDefs or to define it at design-time. But TC says he has dynamic queries so that's not an option

Share this post


Link to post

OT: you write that you are a newcomer. I quickly quit SQLite because of date problems. That was also the recommendation of experienced programmers.
Consider another DB.

Share this post


Link to post
4 hours ago, Fr0sT.Brutal said:

But for this the field must be of type TDateTimeField which AFAIU does not automatically happen for SQLite as the engine can't know whether an integer field is timestamp or just integer. So user has to create the field at runtime doing tricks with FieldDefs or to define it at design-time. But TC says he has dynamic queries so that's not an option

If you use a suitable column type alias in your SQLite table definition, FireDAC will figure out the correct field data type to use.

 

DATE | SMALLDATE dtDate
DATETIME | SMALLDATETIME dtDateTime
TIMESTAMP dtDateTimeStamp
TIME dtTime

 

https://docwiki.embarcadero.com/RADStudio/Alexandria/en/SQLite_support_in_RAD_Studio

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×