I have not used SQLAlchemy (I just use sqlite3 in my projects). It seems to me that SQLAlchemy is a python layer on top of a number of database implementations or ādialectsā. I would have assumed that its API would expect datetime.date
objects for dates, irrespective of how any dialect stores them.
Have you tried converting the wx.DateTime
object returned by the DatePickerCtrl
into a datetime.date
object and passing the result to SQLAlchemy?
I use the following function to convert wx.DateTime
objects into datetime.date
objects:
import wx
import datetime
def wxDateTime2PyDate(wx_datetime):
"""Convert a wx.DateTime to a datetime.date.
:param wx_datetime: a wx.DateTime object.
:raise: ValueError, if the date is not valid.
:return: a datetime.date object.
"""
assert isinstance(wx_datetime, wx.DateTime)
if wx_datetime.IsValid():
ymd = map(int, wx_datetime.FormatISODate().split('-'))
return datetime.date(*ymd)
else:
raise ValueError("Invalid date")
Example:
wx_dt = wx.DateTime.Now()
py_date = wxDateTime2PyDate(wx_dt)
print(type(wx_dt), wx_dt)
print(type(py_date), py_date)
Which outputs:
<class 'wx._core.DateTime'> Thu Apr 6 10:53:05 2023
<class 'datetime.date'> 2023-04-06
However, if the SQLAlchemy API really does expect dates to be passed as strings, then you would need to know what format it expects. If it expects the ISO 8601
format "YYYY-MM-DD"
, then you could just call the wx.DateTime
objectās FormatISODate()
method.