Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I cannot add the "date" to the database as it is not compatible with QDate



  • I have a QDateEdit field on my form and every time I try to add this value to the database an error is returned:

    sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'QDate'
    

    According to the message, the QDate type is not supported by the database (Postgresql).
    I've tried to convert to string like this:

        self.date = self.view_date.data.date()
        self.date = str(self.date)
    

    Does anyone have any idea how I can get around this?



  • @_jao_victor_
    You don't help us by failing to say whether you are using PyQt or PySide. And FWIW I don't think the error comes from the database, I think it comes from the SQLAlchemy library you are choosing to use.

    • Try QDateTime instead of QDate: maybe your variable type allows self.date = self.view_date.data.datetime().
    • Try forcing conversion to Python datetime: self.date = datetime.datetime(self.view_date.data.date()).
    • Look up the documentation for Postgresql/SQLAlchemy to see what exactly it accepts for a date/time: e.g. str(self.date) might be along the right lines but may require a particular format, not the default.


  • @JonB The text () method solves my problem, as it returns the exact string as an example: 1/31/2021.
    So stay:

    self.date = self.view_date.data.text()
    


  • @_jao_victor_ said in I cannot add the "date" to the database as it is not compatible with QDate:

    solves my problem

    if your issue is solved, please don't forget to mark your post as such!


Log in to reply