Qt World Summit: Register Today!

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
    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");
        mDatabase = QSqlDatabase::addDatabase("QMYSQL", "Conexion1");
        }else if(!nDatabase.open()){

  • 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/3

    What os are you actually on?

    Depending on that your libraries will have a different file ending (.dylib, .so, .dll)

    I would to following steps

    1. navigate to your Qt folder into the plugins/sqldriver directory
    2. 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 called Depends.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)
    1. Check if libmysqlclient.XX.FILE-ENDING is existing in the named path.
    2. 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

  • @Fuel @xhallix Sorry :S How silly of me. I forgot to use equal(=) instead of dot(.) Now i dont get error messages

     nDatabase=QSqlDatabase::addDatabase("QMYSQL", "Conexion2");

Log in to reply