2

I'm developing a PyQT application and want to use a SQL database. The engine doesn't matter that much. And I have a working MySQL installed in my system (I use it frequently), and also the SQLite3 DLL is present, by default in my system32 folder, and I have also put a copy beside qsqlite dll.

But when I call

db = QtSql.QSqlDatabase.addDatabase("QSQLITE")

or

db = QtSql.QSqlDatabase.addDatabase("QMYSQL")

All I get is:

QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:

No Drivers at all. What should I do? Thanks in advance.

arashka
  • 1,226
  • 3
  • 17
  • 30

2 Answers2

5

I use PySide to develop application with Python 3.3. I also encounter the same problem, and I have a workaround.

I found that if QApplication() is called before QtSql.QSqlDatabase.addDatabase('QSQLITE'), the problem is gone.

Finally, I try the method to place the following codes before addDatabase and the problem is solved

site_pack_path = site.getsitepackages()[1]
QtGui.QApplication.addLibraryPath('{0}\\PySide\\plugins'.format(site_pack_path))

ps. check that the "sqldrivers" folder is in 'plugins' folder

cigar huang
  • 172
  • 1
  • 5
  • I had the same problem (except it was not PyQt but PySide) and was getting completely hopeless. Your solution saved me! Thanks a lot indeed. – HiFile.app - best file manager Apr 27 '14 at 08:25
  • 1
    @cigar huang The question is why is this needed? Shouldn't this work seamlessly? I feel like fixes like this (which worked for me, incidentally, so +1), make me feel like I'm engaging in superstitious behavior, cargo cult programming. Putting stuff in without really appreciating why it is needed in the first place. – eric Oct 30 '14 at 13:20
  • 1
    @neuronet referece [link](http://www.cnblogs.com/ungshow/archive/2010/10/10/1847082.html) It explains why solution works, but it is simpled-chinese. Qt application searches for plugin in the folders, which are beside .exe, or in library path. Above solution adds plugins folder of Pyside into library path, and 'sqldrivers' is just in it. Another solution needn't add library path, instead of copying 'sqldriver' folder to the same location of .exe or .py – cigar huang Nov 07 '14 at 07:51
  • Solved my problem when using PyQt5 in virtual env. Thank you! – uetoyo Jan 14 '16 at 11:26
1

I don't speak english very well.

I use Python 2.7 under windows 10, Postgresql, and developt the gui in QT , use PyQt4 and QtSql. And use spyder. (In reality everything is included in Python ( x , y))

,-) but here is the easy solution. Open the file C:\Python27\qt.conf, First I had inside the file this lines:

Prefix = C:/Python27/Lib/site-packages/PyQt4
Binaries = C:/Python27/Lib/site-packages/PyQt4

then you add the next lines:

Plugins = C:/Python27/Lib/site-packages/PyQt4/plugins
Translations = C:/Python27/Lib/site-packages/PyQt4/translations

Make sure that the plugins are in C:/Python27/Lib/site-packages/PyQt4/plugins. Then close python and open again. Then all work perfectly. You found the same answer here http://www.voidynullness.net/blog/2013/01/24/pyqt-database-driver-loading-issues-on-windows-after-installing-pyside/

Gabriel Asqui
  • 305
  • 3
  • 17
  • If you use ANACONDA 5.0.0 in Windows 10, this does not have installed drivers for default. You should open a terminal and use 'pip install PyQt5'. Then you have the drivers. – Gabriel Asqui Oct 08 '17 at 17:27