Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to obtain a pointer to sqlite3 handle in PyQt5's binding of QtSqlDriver
I'm trying to register some functions in sqlite within my PyQt5 app. To do so within Python, I'm trying to obtain the pointer (actually,
sqlite3 **) to the sqlite instance, so I could use sqlite3_create_function through Python's ctypes.
When doing this in Qt5 instead of Python, you just use QSqlDriver::handle(), which returns a
QVariantcontaining the address. You cast the
QVariant.data()appropriately and off you go. When I try to obtain the handle in Python , I get:
db.driver().handle() TypeError: unable to convert a C++ 'sqlite3*' instance to a Python object
I found that this error is generated within the C++ part of qpycore. The code shows that a
QVariantcontaining a pointer to an unknown type cannot be translated to a Python object. However, I don't need a Python object, just a pointer I can use with
Does anyone know of a way to do this? Thanks!
JonB last edited by
If you do not get an answer to this here, you should go to https://riverbankcomputing.com/mailman/listinfo/pyqt and join the PyQt mailing list for this question, where I think you will find it will be answered.
Denni 0 Banned last edited by Denni 0
Okay I use sqlite3 with python but not with pyqt5 because the Model Class that I use to encapsulate all my database communication is done strictly in python and then it gets called by the Controller based on requests from the 2 other Classes that need data from it one of those being the View (PyQt5 GUI). This alleviates any issues like the one you seem to be experiencing because it uses the MVC methodolgy where in the View knows nor cares about the Data Source (Model) and the Data Source knows nothing about how the data it provides is consumed/used via the View (Gui) or whatever else might be using that data.
This is to say perhaps you might want to revisit your design instead as a proper base design alleviates numerous unnecessary issues that will occur otherwise.
I considered doing that, writing a Python-only model class using Python's native
sqlite3library or even
sqlalchemy, but I'm afraid it would be considerably less performant than QSqlQueryModel and QSqlTableModel. This would also require some serious changes to my app (say, the way variables are bound to queries is Qt-specific), so I'd rather keep using the Qt models if possible.
Denni 0 Banned last edited by Denni 0
Yes but the AbstractModels can be used in place of the SQLModels -- and most important you do not tightly bind your View to your Data Source such that you can easily make changes to either without effecting the other -- including completely replacing one with the another version.
For instance you start with a PC Application using a sqlite3 database but it takes off and they now want a web version as well and instead of sqlite3 they want to go with Oracle. Now you have to rewrite everything because you have to completely change those bindings -- where if you had done it MVC you could have just replaced the sqlite3 with any database seamlessly as well as just added that web based front end because the calls used by the GUI would be the same regardless of what kind of GUI you were rendering. Their is a major reason MVC is the much more preferred development methodology these days. I mean you are coding using something that came about due to this concept -- as Python was created to make things more modular than even Object Oriented coding and Object Oriented came about by trying to make reusable code and thus creating more generic classes that can used by anything by sub-classing. Basically the concept of modularity is actually the most effective to-date method of coding just about anything -- except for perhaps the extremely precision coding which is done in straight C ;)
Note I did a lot of generalization in the above paragraph as I do realize there are other reasons as well but I am just speaking to one aspect of things.
But to each their own just trying to help you out because eventually that is going to come back and bite someone.