QtSQL - QMYSQL driver not loaded (Ubuntu)

2019-08-07 21:24发布

问题:

I want to access a SQL database via PyQt4 on Ubuntu. I already installed apt-get install python-qt4-sql but when I add a Database like this:

import sys
from PyQt4 import QtGui, QtCore, QtSql

appcore = QtCore.QCoreApplication(sys.argv)

db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
db.setHostName('localhost')
db.setDatabaseName('database1')
db.setUserName('root')
db.setPassword('root')

if db.open() == False:
    print 'fail'

I get the error

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE

How can I load the driver QMYSQL?

回答1:

I could fix the problem by installing apt-get install libqt4-sql-mysql.