I know there a many questions and answers on SO for this question, but nothing worked for me.
I installed the newest QT Creator 4.9.2
with Qt 5.13.0
MinGW 32-Bit
and MySQLServer 5.5
to compile the QMYSQL
driver. I but the driver in the sqldrivers
folder and the libmysql.dll
in the bin folder.
If I use C++ to connect to the database, everything is working fine, but I doesn't with python.
Additionally I installed on a fresh Python 3.7 32-Bit:
- PyQt5:
pip install pyqt5 (5.13.0)
- mysqlclient:
pip install mysqlclient (1.4.2)
- mysql-connector:
pip install mysql-connector (2.2.9)
and but the driver I compiled before in the sqldrivers folder from PyQt5, because the driver was missing there as well.
My Class:
class SqlConnector(QSqlDatabase):
def __init__(self):
super(SqlConnector, self).__init__()
self.db = self.connect()
def __del__(self):
self.db.close()
def connect(self):
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("hostname")
db.setDatabaseName("database")
db.setUserName("username")
db.setPassword("1234567890")
ok = db.open()
if ok:
print("Connection established")
return db
else:
print("Connection failed: ", db.lastError().text())
return None
def getPools(self):
query = QSqlQuery("SELECT id, name, token, dlxCode FROM AnalysisPool", self.db)
model = QSqlQueryModel()
model.setQuery(query)
return model
Main Method:
from PyQt5.QtSql import QSqlDatabase, QSqlQueryModel, QSqlQuery
from PyQt5.QtWidgets import QApplication, QTableView
if __name__ == "__main__":
app = QApplication(sys.argv)
poolView = QTableView()
poolView.show()
database = SqlConnector()
# poolView.setModel(database.getPools())
sys.exit(app.exec_())
The libraryPaths from PyQt are C:/Python/lib/site-packages/PyQt5/Qt/plugins
and C:/Python
The used OS is Windows 10 (1809) 64-Bit
and I included in the path variable the MySQLServer lib folder.
I don't know what to do, I have read every SO question about this topic without a working solution for my problem.