Search code examples
pythonqtpyqtodbcpyside

PySide + QtSql - cannot load database drivers


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.


Solution

  • 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))