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

converting from mySQL to SQlite with QSqlTableModel



  • Hello,

    I would appreciate your help with the following problem:
    I have written an application with pyQt5 that uses a mysql database
    together with QSqlTableModel. I decided to migrate to sqlite and managed
    to migrate the data in the database. But this means that the dates from the
    sqlite database are now QStrings and not QDate as before.
    This makes my code crash, as the code often expects QDate instead of QString.
    Is there an easy way to fix this? Can I somehow make Qt convert the date strings from the sqlite database into a QDate?
    Of course I could go through my code line by line and change it everywhere is wants to have a QDate, but that would be very tedious. Is there a better way?

    thanks for your help!


  • Lifetime Qt Champion

    Hi,

    And what is your question exactly ?



  • Sorry, I made a mistake and left my question empty. Thank you for the prompt response.



  • just to clarify, database access is handled with Qt5 and not with python. I use QSqlDatabase to register the mySQL and later SQlite databases.


  • Lifetime Qt Champion

    How did you do the migration ?



    • subclass QSQLiteResult and reimplement bindValue to convert dates to strings and data() to convert strings to dates.
    • create a custom driver squbclassing QSQLiteDriver and reimplementing createResult() to return an instance of the subclass created above.


  • I did the migration with mysqldump then I edited the dump file. Then I have the resulting file into sqlite and saved it as database. The date columns are now stored in sqlite as TEXT fields. All done on a linux machine.

    Thank you VRonin for your suggestion. That sounds like a great idea. Can I do this in pyQt5 or do I need to go to C++? I think QSQLiteDriver is a plugin i.e. a shared library. That means I have to do this in C++. Is that right? I wanted to avoid C++ so that my application is more portable. But anyway this solution sounds great.

    Thanks for you answers guys.