Problem when trying to connect to 2 databases
-
I have 2 connections ("conexion1" and "conexion2"), however, only "conexion1" works. I get the following message when i run it. Message Box: Driver not loaded Driver not loaded
qDebug:
Driver not loaded Driver not loaded
QSqlQuery::value: unknown field name ''
QSqlQuery::value: unknown field name ''
QSqlQuery::value: unknown field name ''
QSqlQuery::value: unknown field name ''
QSqlQuery::value: unknown field name ''
QSqlQuery::value: unknown field name ''nDatabase.QSqlDatabase::addDatabase("QMYSQL", "Conexion2"); nDatabase.setHostName("127.0.0.1"); nDatabase.setDatabaseName("inventary1"); nDatabase.setPort(3306); nDatabase.setUserName("root"); nDatabase.open(); mDatabase = QSqlDatabase::addDatabase("QMYSQL", "Conexion1"); mDatabase.setHostName("localhost"); mDatabase.setDatabaseName("Inventary2"); mDatabase.setPort(3306); mDatabase.setUserName("root"); mDatabase.open(); if(!mDatabase.open()){ QMessageBox::information(this,"conexion1",mDatabase.lastError().text()); }else if(!nDatabase.open()){ QMessageBox::information(this,"conexion2",nDatabase.lastError().text()); qDebug()<<"error"<<nDatabase.lastError().text(); }
-
i dont think that both Connections are working, because the Driver is not loaded. In most cases the used Driver is to old in QT and you use a newer Version of MySQL Server. So you need to compile your own Driver with QT and your MySQL Libs you are using.
Download the Sources from QT and search for the MySQL Driver Files. At the Moment i dont know where they are, but you need to run the qmake Command in this folder. -
as @Fuel said, your connection does not work, because you do not have the correct mysql driver (libs) for your system.
I had a similar problem on osx -> https://forum.qt.io/topic/79588/use-qmysql-driver-on-without-having-mysql-installed/3What os are you actually on?
Depending on that your libraries will have a different file ending (
.dylib
,.so
,.dll
)I would to following steps
- navigate to your Qt folder into the
plugins/sqldriver
directory - Now it depends on your OS, check if all dependencies are met.
osx: use$ otool -L libqsqlmysql.dylib
,
linux:$ ldd libqsqlmysql.so
,
windows: there is a tool calledDepends.exe
Now you see the dependencies
libqsqlmysql.dylib (compatibility version 0.0.0, current version 0.0.0) @rpath/QtSql.framework/Versions/5/QtSql (compatibility version 5.8.0, current version 5.8.0) @rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.8.0, current version 5.8.0) /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0) /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0) /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5) /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
- Check if
libmysqlclient.XX.FILE-ENDING
is existing in the named path. - I guess it will be not, so copy the library file from your mysql folder in here, so that QT is able to use the driver by looking up the lib.
Maybe this saves you some time
- navigate to your Qt folder into the