I can't connect to my local database.



  • [ SOLVED ]

    Hello,
    I want to connect to my local database from QT but when I run my project it gives me this error from db.lasterror().texet() :
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

    My OS is Centos 7 64 Bit.
    Please help.



  • @leonidas_lolo said:

    QSqlDatabase: QMYSQL driver not loaded

    Take a look on this thread:
    QSqlDatabase: QMYSQL driver not loaded but available



  • I did what the thread said but it didn't worked. my code is :
    main.cpp file

    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setPort(3306);
        db.setUserName("thename");
        db.setPassword("thepassword");
        db.setDatabaseName("thedatabase");
        qDebug()<<db.open();
        return a.exec();
    }
    

    my .pro file have the followings :

    QT       += core
    QT       += sql
    QT       -= gui
    TARGET = DB_Programming
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    

    Please advise.

    Edited: Put code after ``` (3 backticks) and close with the same - p3c0



  • @leonidas_lolo Hi, normally everything should be working out of the box after installing Qt and the MySQL database.
    Just to double-check:

    • you added a line like qDebug() << "Plugins loaded from: " << QCoreApplication::libraryPaths(); to make sure the plugins path with the sqldrivers subdirectory is in there (it should, otherwise you won't get the list containing QMYSQL in "available drivers")
    • you ran ldd /usr/lib/qt/plugins/sqldrivers/libqsqlmysql.so to check if there is an entry like libmysqlclient.so.18 => /usr/lib/libmysqlclient.so.18 (0xb738d000)

    One other thing that just came to my mind: You don't have multiple versions of MySQL installed or the like (e.g. MySQL and MariaDB)?



  • I do not have experiance with MySQL but with PostrgeSQL the problem (every time) was that the PostgreSQL libs were not available to the application. We had to either copy the dlls into the same folder as the application or make it avaiable in the PATH (windows)

    So on Linux make sure that the libs are located in the LD_LIBRARY_PATH



    • I added the line to my .cpp file
      qDebug() << "Plugins loaded from: " << QCoreApplication::libraryPaths(); and it says :
      ("/opt/Qt/5.4/gcc_64/plugins", "/home/<username>/Documents/Qt Projects/<another folder>/build-DB_Programming-Desktop_Qt_5_4_0_GCC_64bit-Release")
      I searched in that directory and I do have the folder sqldrivers.

    The file libqsqlmysql.so is locate at my installation directory /opt/Qt/5.4/gcc_64/plugins/sqldrivers.
    I make a copy and paste it to the previous direcotry but did not wokr either.

    When I ran the ldd on libqsqlmysql.so on the /opt/Qt/5.4/gcc_64/plugins/sqldrivers location it says:
    linux-vdso.so.1 => (0x00007fffdfd11000)
    libmysqlclient_r.so.16 => not found
    libQt5Sql.so.5 => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/libQt5Sql.so.5 (0x00007f4f00daf000)
    libQt5Core.so.5 => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/libQt5Core.so.5 (0x00007f4f00673000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f4f0036c000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f4efffaa000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4effd8e000)
    libicui18n.so.53 => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/libicui18n.so.53 (0x00007f4eff942000)
    libicuuc.so.53 => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/libicuuc.so.53 (0x00007f4eff5b6000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f4eff3b2000)
    libgthread-2.0.so.0 => /lib64/libgthread-2.0.so.0 (0x00007f4eff1b0000)
    librt.so.1 => /lib64/librt.so.1 (0x00007f4efefa7000)
    libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007f4efec7e000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f4efe97c000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f4efe765000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f4f0121d000)
    libicudata.so.53 => /opt/Qt/5.4/gcc_64/plugins/sqldrivers/../../lib/libicudata.so.53 (0x00007f4efd0dd000)


  • Moderators

    @leonidas_lolo

    libmysqlclient_r.so.16 => not found

    install mysqlclient libraries.



  • I run the command locate mysqlclient and I have the libraries.

    [username@localhost ~]$ locate mysqlclient
    /usr/lib64/libmysqlclient.so
    /usr/lib64/libmysqlclient.so.18
    /usr/lib64/libmysqlclient.so.18.1.0
    /usr/lib64/libmysqlclient_r.so
    /usr/lib64/libmysqlclient_r.so.18
    /usr/lib64/libmysqlclient_r.so.18.1.0
    /usr/lib64/mysql/libmysqlclient.so
    /usr/lib64/mysql/libmysqlclient.so.18
    /usr/lib64/mysql/libmysqlclient.so.18.0.0
    /usr/lib64/mysql/libmysqlclient_r.so


  • Moderators

    So I think the only way would be to try to link libmysqlclient_r.so.16 to /usr/lib64/libmysqlclient.so.18 Or install old version of mysql client libraries which will contain *.so.16



  • I fixed the problem by re-installing the QT Creator.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.