Insert date from wxdatepickerctrl to database

self.dateEntry = wx.GenericDatePickerCtrl(self, -1, size=(120,20),
pos=(90,185),
                                       style = wx.TAB_TRAVERSAL
                                           > wx.DP_DROPDOWN
                                           > wx.DP_SHOWCENTURY
                                           > wx.DP_ALLOWNONE )
self.dateEntry.Bind(wx.EVT_DATE_CHANGED, self.OnGetDate)

...

def OnGetDate(self, evt):
    date = evt.GetDate()
    return date
...

def AddEntry(self, evt):
...
cur.execute("insert into entries (ref_no, entry_date, description)
values (%s, %s, %s)", (refNo, datetime.date(self.OnGetDate), desc,))

So it says TypeError: an integer is required, I know the problem
causing the error is the datetime.date(self.OnGetDate), date has the
format datetime.date(yyyy, mm, dd), while the datepickerctrl has (mm/
dd/yy). I've tried getting rid of the datetime.date, but another error
comes up saying psycopg2.ProgrammingError: can't adapt type
'instancemethod', and I know its saying that because I cant call a
method to be inserted to the database. So my question is there another
way, to get the date on the widget and place it on the database. I'm
having a problem solving either ways. Or am I doing things wrong.

self.dateEntry = wx.GenericDatePickerCtrl(self, -1, size=(120,20),
pos=(90,185),
style = wx.TAB_TRAVERSAL
> wx.DP_DROPDOWN
> wx.DP_SHOWCENTURY
> wx.DP_ALLOWNONE )
self.dateEntry.Bind(wx.EVT_DATE_CHANGED, self.OnGetDate)

...

def OnGetDate(self, evt):
date = evt.GetDate()
return date
...

def AddEntry(self, evt):
...
cur.execute("insert into entries (ref_no, entry_date, description)
values (%s, %s, %s)", (refNo, datetime.date(self.OnGetDate), desc,))

So it says TypeError: an integer is required, I know the problem
causing the error is the datetime.date(self.OnGetDate), date has the
format datetime.date(yyyy, mm, dd), while the datepickerctrl has (mm/
dd/yy). I've tried getting rid of the datetime.date, but another error
comes up saying psycopg2.ProgrammingError: can't adapt type
'instancemethod', and I know its saying that because I cant call a
method to be inserted to the database. So my question is there another
way, to get the date on the widget and place it on the database. I'm
having a problem solving either ways. Or am I doing things wrong.

The TypeError says that an integer is required, but dates are not
integers unless your date is represented in the database as a Unix
time integer. Could it be that it is another field that is expecting
an integer? Try printing all the types before your cur.execute and
check.

What kind of database is this? I guess it is not SQLite since it
doesn't enforce types, but in my exp with SQLite, you can store dates
of whatever format you want. To go from wxDateTime to a string, just
do

wxdatetime_string = str(my_wxDatetime)

Though I recommend storing dates as strings of the format:
YYYY-MM-DD HH:MM:SS.microsconds

Che

···

On Mon, Aug 29, 2011 at 1:03 PM, karentinedo <kareen.tinedo@gmail.com> wrote:

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

self.dateEntry = wx.GenericDatePickerCtrl(self, -1, size=(120,20),
pos=(90,185),
                                        style = wx.TAB_TRAVERSAL
                                            > wx.DP_DROPDOWN
                                            > wx.DP_SHOWCENTURY
                                            > wx.DP_ALLOWNONE )
self.dateEntry.Bind(wx.EVT_DATE_CHANGED, self.OnGetDate)

...

def OnGetDate(self, evt):
     date = evt.GetDate()
     return date
...

def AddEntry(self, evt):
...
cur.execute("insert into entries (ref_no, entry_date, description)
values (%s, %s, %s)", (refNo, datetime.date(self.OnGetDate), desc,))

1. You are trying to create a datetime from a method object, not a value. IOW, you forgot a () after OnGetDate.

2. OnGetDate is being bound as an event handler and then later it seems that you want to call it to fetch the value. Perhaps instead you should save the data in a instance variable in OnGetDate when the event happens, and then have another getter (or just use the saved value) when you are inserting it into the database.

So it says TypeError: an integer is required, I know the problem
causing the error is the datetime.date(self.OnGetDate), date has the
format datetime.date(yyyy, mm, dd), while the datepickerctrl has (mm/
dd/yy).

Those are just the default string representations of the two types of date classes. Each of them actually has a lot more content and functionality than a plain string.

I've tried getting rid of the datetime.date, but another error
comes up saying psycopg2.ProgrammingError: can't adapt type
'instancemethod', and I know its saying that because I cant call a
method to be inserted to the database.

Again, the problem is probably that you are not calling it. You are passing it directly and the db module doesn't know how to store or adapt a method object.

So my question is there another
way, to get the date on the widget and place it on the database. I'm
having a problem solving either ways. Or am I doing things wrong.

IIRC psycopg2 is able to handle Python datetime objects correctly, adapting them to/from the native database's date representation, so all you really need is to convert a wx.DateTime to a datetime.date. You can do that with code like this:

     def convertDate(wxdt):
         date = datetime.date(wxdt.GetYear(),
                              wxdt.GetMonth()+1,
                              wxdt.GetDay())
         return date

Oh, and remember to be sure to pass the return value of this function to the db, not the function itself. :wink:

···

On 8/29/11 10:03 AM, karentinedo wrote:

--
Robin Dunn
Software Craftsman