Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I can't connect to my local database.
QtWS25 Last Chance

I can't connect to my local database.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leonidas_lolo
    wrote on last edited by leonidas_lolo
    #1

    [ 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.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hpollak
      wrote on last edited by
      #2

      @leonidas_lolo said:

      QSqlDatabase: QMYSQL driver not loaded

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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leonidas_lolo
        wrote on last edited by p3c0
        #3

        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

        simowS 1 Reply Last reply
        0
        • L leonidas_lolo

          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

          simowS Offline
          simowS Offline
          simow
          wrote on last edited by simow
          #4

          @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)?

          Let's Try To Negotiate!

          1 Reply Last reply
          0
          • TheBadgerT Offline
            TheBadgerT Offline
            TheBadger
            wrote on last edited by
            #5

            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


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leonidas_lolo
              wrote on last edited by
              #6
              • 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.

              • I don't have any file libqsqlmysql.so on the /usr/lib/qt4/plugins/sqldrivers.
                The only file i have is libqsqlite.so.

              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)

              p3c0P 1 Reply Last reply
              0
              • L leonidas_lolo
                • 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.

                • I don't have any file libqsqlmysql.so on the /usr/lib/qt4/plugins/sqldrivers.
                  The only file i have is libqsqlite.so.

                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)

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @leonidas_lolo

                libmysqlclient_r.so.16 => not found

                install mysqlclient libraries.

                157

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leonidas_lolo
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    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

                    157

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      leonidas_lolo
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved