3

Edit: I had to change the question because I found that the problem related not only to ODBC driver bu to all drivers such as MYSQL, SQLITE etc.

So the problem is this: I try to connect to a database using PySide and QtSql module.

The critical snippet:

from PySide import QtGui
from PySide import QtSql
import sys

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    db = QtSql.QSqlDatabase.addDatabase('QODBC')  
    # or others: QMYSQL, QSQLITE etc.
    sys.exit(app.exec_())

and when I try to run it, I get a message:

QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers: 

When I try equivalent PyQt4 instead of PySide, I get no message which I assume means that the ODBC driver is available with PyQt4. However, unfortunately, I cannot switch my application from PySide to PyQt4 for many other reasons.

When I look into Python Lib folder I can see that python-3.3.2\Lib\site-packages\PySide\plugins\sqldrivers contains DLLs including qsqlodbc4.dll. So there is something more missing and I do not know what.

As I wrote the problem is not just with ODBC but with all database drivers.

2
  • what is the version of PySide ? For PySide older than 1.2.0 you need to call the PySide post-install script after installing, to generate the qt.conf file. For example: c:\Python33\Scripts\python.exe c:\Python33\Scripts\pyside_postinstall.py -install Commented Apr 26, 2014 at 19:47
  • It is version 1.2.1. It was bundled and installed with WinPython 3.3.2.3. When I look at intsllation log, it tells me that the post-install script was run. Even after I run it manullay myself, the problem with ODBC still exists. Commented Apr 27, 2014 at 7:08

2 Answers 2

1

Found a solution here on stackoverflow here (relates to PyQt rather than PySide but it works): PyQT can't find any SQL drivers

In short - put this code before calling addDatabase:

site_pack_path = site.getsitepackages()[1]
QtGui.QApplication.addLibraryPath('{0}\\PySide\\plugins'.format(site_pack_path))
Sign up to request clarification or add additional context in comments.

2 Comments

this is what the post-install script should do. Maybe there is some bug in post-install script. Could you run the script and send me here the output ? thanks
@V.K Any word on why this fix is even needed? It seems like a bug. Has it been reported to PySide, do you know?
0

If you encounter a similar issue using modern PySide6, here is the full answer: https://stackoverflow.com/a/72169911/211369

One of solutions is to define the QT_PLUGIN_PATH environment variable, so that it contains the "sqldrivers" sub-directory with SQL plugins for Qt platform.

In your example, QT_PLUGIN_PATH=c:/python-3.3.2/Lib/site-packages/PySide/plugins

BTW almost a half of info known to me is is officially published at https://doc.qt.io/qt-6/deployment-plugins.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.