Write and read dates and times to/from an SQLite file
Posted: Sat Oct 07, 2023 6:01 am
Hello everyone,
In my project, the user will click the "Save" button on a form which writes the record to the SQLite record id determined previously by last_insert_rowid.
A DateGadget is placed on the form with the code below that the user should be able to modify if required:
The form has a "Save" button that when pressed, collects the entries in the small number of fields and writes them into the SQLite file using the code below:
I'm using a TEXT field in the SQLite file to store the date/time.
The problem is that I don't think I'm handling the date/time part correctly as I've seen the SQLite "datetime" function documented elsewhere and I think I'm supposed to use it. I've had a lot of goes at this with varying results and I'm wondering if someone could give me some guidance please on the correct way to write and read dates and times to and from a SQLite file. Also, another part of the code reads and displays the SQLite records in a ListIconGadget with the most recent record at the top. I might also need to offer the user the choice of sorting the records by date/time in ascending or descending order so I'm needing to get this right from the start.
Any help regarding handling dates and times in SQLite would be appreciated, thank you.
In my project, the user will click the "Save" button on a form which writes the record to the SQLite record id determined previously by last_insert_rowid.
A DateGadget is placed on the form with the code below that the user should be able to modify if required:
Code: Select all
myCalendar = DateGadget(#PB_Any, 80, 20, 150, 20, "%yyyy/%mm/%dd %hh:%ii")
Code: Select all
OpenDatabase(sqliteNo, "mySQLiteFile.db", "", "")
SetDatabaseString(sqliteNo, 0, GetGadgetText(myCalendar))
SetDatabaseString(sqliteNo, 1, GetGadgetText(strField1))
SetDatabaseString(sqliteNo, 2, GetGadgetText(strField2))
SetDatabaseString(sqliteNo, 3, GetGadgetText(strField3))
SetDatabaseString(sqliteNo, 4, GetGadgetText(strField4))
SetDatabaseString(sqliteNo, 5, GetGadgetText(strField5))
SetDatabaseLong(sqliteNo, 6, GetGadgetState(cbxFrmEventAttention)) ;CheckBoxGadget gadget
SetDatabaseString(sqliteNo, 7, "idle")
SetDatabaseLong(sqliteNo, 8, lastRecID)
DatabaseUpdate(sqliteNo, "UPDATE recordEvents SET eventTime=?, eventStaff=?, eventTitle=?, internalCase=?, externalCase=?, eventDescription=?, attention=?, editStatus=? WHERE id=?")
CloseDatabase(sqliteNo)
The problem is that I don't think I'm handling the date/time part correctly as I've seen the SQLite "datetime" function documented elsewhere and I think I'm supposed to use it. I've had a lot of goes at this with varying results and I'm wondering if someone could give me some guidance please on the correct way to write and read dates and times to and from a SQLite file. Also, another part of the code reads and displays the SQLite records in a ListIconGadget with the most recent record at the top. I might also need to offer the user the choice of sorting the records by date/time in ascending or descending order so I'm needing to get this right from the start.
Any help regarding handling dates and times in SQLite would be appreciated, thank you.