AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt Delphi Tip of the Day: Prevent SQLite Date Headaches by using a GetDateAs_YYYYMMDD Fu
Thema durchsuchen
Ansicht
Themen-Optionen

Delphi Tip of the Day: Prevent SQLite Date Headaches by using a GetDateAs_YYYYMMDD Fu

Ein Thema von DP News-Robot · begonnen am 5. Jan 2024
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
14.979 Beiträge
 
#1

Delphi Tip of the Day: Prevent SQLite Date Headaches by using a GetDateAs_YYYYMMDD Fu

  Alt 5. Jan 2024, 12:40
Working with Date data can be very tricky. I recently encountered an "Invalid argument to date encode", error while trying to update a SQLite database table.



This placed a value of 0000-00-00 into the date field of my SQLite table.

Here is the original code which caused the error.



function TForm1.GetOneOffDateAsDate: TDate; begin Result := DateEdit1.Date; end; procedure TForm1.SQLInsertPayment; begin qryO.SQL.Clear; qryO.SQL.Add( ' INSERT INTO OneOffPayments '); qryO.SQL.Add( ' ( '); qryO.SQL.Add( ' OneOffDate, '); qryO.SQL.Add( ' OneOffPayment '); qryO.SQL.Add( ' ) '); qryO.SQL.Add( ' VALUES ( '); qryO.SQL.Add( ' :ood, '); qryO.SQL.Add( ' :oop '); qryO.SQL.Add( ' ); '); qryO.ParamByName( 'ood' ).Value := GetOneOffDateAsDate; qryO.ParamByName( 'oop' ).Value := GetOneOffAmount; qryO.ExecSQL; end;

The getter function GetOneOffDateAsDate passes in a TDate which doesn't play nicely with FireDAC. Fortunately, the fix is quite simple. I found a fantastic explanation for this error on stackoverflow which states FireDAC expects DATE data type values to be a string in the fixed format of YYYY-MM-DD.

FireDAC Expects DATE data types
to be strings formatted as YYYY-MM-DD


So I created another getter function to format the date data as a YYYY-MM-DD string.
Problem solved!

Updated code passing FireDAC a YYYY-MM-DD string



function TForm1.GetOneOffDateAs_YYYYMMDD: String; begin Result := FormatDateTime('YYYY-MM-DD', DateEdit1.Date); end; procedure TForm1.SQLInsertPayment; begin qryO.SQL.Clear; qryO.SQL.Add( ' INSERT INTO OneOffPayments '); qryO.SQL.Add( ' ( '); qryO.SQL.Add( ' OneOffDate, '); qryO.SQL.Add( ' OneOffPayment '); qryO.SQL.Add( ' ) '); qryO.SQL.Add( ' VALUES ( '); qryO.SQL.Add( ' :ood, '); qryO.SQL.Add( ' :oop '); qryO.SQL.Add( ' ); '); qryO.ParamByName( 'ood' ).Value := GetOneOffDateAs_YYYYMMDD; qryO.ParamByName( 'oop' ).Value := GetOneOffAmount; qryO.ExecSQL; end;

Happy coding!



Enjoy!
Gunny Mike
https://zilchworks.com



Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:53 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz