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. Cannot connect to databases, Driver not loaded error.
Forum Updated to NodeBB v4.3 + New Features

Cannot connect to databases, Driver not loaded error.

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 4.6k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    mleoni
    wrote on 13 May 2018, 18:04 last edited by
    #1

    Hello everyone, this is yet another post on the dreaded error when trying to connect to a database from Qt, but a bit different.

    I am on an up-to-date Debian testing with Qt installed to /opt from the online installer.
    I am trying to write a program that connects to a database but the connection fails with error "Driver not loaded". Now, I've seen a lot of similar discussions but I wish to point out that this one is different from the ones that I've seen so far. In particular, I used to have the "regular" error due to a linking issue in libqsqlmysql.so but then I followed instruction online, recompiled it an substituted the original file.
    After that, the error message that I get changed and now my code, which reads

    db.addDatabase("QMYSQL");
    db.setDatabaseName("SomeDatabase");
    if (not db.open())
    {
        qDebug() << db.lastError().text();
    }
    

    simply prints

    "Driver not loaded Driver not loaded"
    

    I thought it was a problem with MySQL so I tried to connect to an SQLite database but to my surprise I got the same error message, meaning that there's something wrong elsewhere.

    Since I'm on Debian, I thought I would try again using Qt from Debian's repositories, so I installed all the missing development packages, added a Qt toolkit to QtCreator and tried again: same error.

    I know that the database is working because I also have a PyQt5 [again from Debian's repositories] application that connects to a similar database by just

    db = QSqlDatabase.addDatabase("QMYSQL")
    db.setDatabaseName("SomeDatabase")
    

    and it works.

    What can I try next? Any clue is welcome.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 May 2018, 18:31 last edited by
      #2

      I still think the sql plugin has some issues. Take a look what http://doc.qt.io/qt-5/qsqldatabase.html#drivers is telling you and make sure libqsqlmysql.so finds all it's dependencies (e.g. with ldd)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 13 May 2018, 19:22 last edited by
        #3

        Hi and welcome to devnet,

        To add to @Christian-Ehrlicher, this question has been asked many times already on this forum, please take the time to search through it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • M Offline
          M Offline
          mleoni
          wrote on 13 May 2018, 20:50 last edited by
          #4

          Hi Christian, SGaist, thank you for your answers. As I mentioned in my post, I have already searched thoroughly the net for a solution, I have already re-compiled libqsqlmysql.so and calling ldd on it shows that not only does it not miss any dependency, it also links to the same libraries as the homonymous file from Debian's repositories. In particular, it links to the installed version of libmariadbclient [Debian switched to MariaDB which is a fork of MySQL and a drop-in replacement].

          In any case, I also mentioned in my previous post that the problem is not specific to the MySQL driver, but I get the same error even if I try to use SQLite as a backend.

          What is the next thing that I can try?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 13 May 2018, 20:56 last edited by
            #5

            Then start your application with the QT_DEBUG_PLUGINS environment variable set to 1. That will give you some more information about what is happening.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mleoni
              wrote on 13 May 2018, 21:12 last edited by
              #6

              Thanks for the suggestion!
              This is the relevant output

              QFactoryLoader::QFactoryLoader() checking directory path "/opt/Qt/5.10.1/gcc_64/plugins/sqldrivers" ...
              QFactoryLoader::QFactoryLoader() looking at "/opt/Qt/5.10.1/gcc_64/plugins/sqldrivers/libqsqlite.so"
              Found metadata in lib /opt/Qt/5.10.1/gcc_64/plugins/sqldrivers/libqsqlite.so, metadata=
              {
                  "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
                  "MetaData": {
                      "Keys": [
                          "QSQLITE"
                      ]
                  },
                  "className": "QSQLiteDriverPlugin",
                  "debug": false,
                  "version": 330241
              }
              
              
              Got keys from plugin meta data ("QSQLITE")
              QFactoryLoader::QFactoryLoader() looking at "/opt/Qt/5.10.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
              Found metadata in lib /opt/Qt/5.10.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so, metadata=
              {
                  "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
                  "MetaData": {
                      "Keys": [
                          "QMYSQL3",
                          "QMYSQL"
                      ]
                  },
                  "className": "QMYSQLDriverPlugin",
                  "debug": false,
                  "version": 330241
              }
              
              
              Got keys from plugin meta data ("QMYSQL3", "QMYSQL")
              
              
              loaded library "/opt/Qt/5.10.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
              "Driver not loaded Driver not loaded"
              

              Meanwhile I have run the Qt example sqlbrowser and it does successfully connect to my MySQL local server, it shows me the database and the tables. This makes me suspect that there is an error in the code, but what could it be?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 May 2018, 21:13 last edited by
                #7

                Debug VS Release version of the plugin ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mleoni
                  wrote on 13 May 2018, 21:22 last edited by
                  #8

                  Sorry, I don't understand the question :(
                  Are you asking me to compare two different builds of the same plugin?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 14 May 2018, 22:04 last edited by
                    #9

                    Did you build your plugin for both debug and release ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply 15 May 2018, 13:08
                    0
                    • H Offline
                      H Offline
                      Homer2000
                      wrote on 15 May 2018, 12:42 last edited by
                      #10

                      need dynamic lib for mysql. For linux it's libmysqlclient.so.16 or libmysqlclient.so.18 for your target platform.

                      1 Reply Last reply
                      0
                      • S SGaist
                        14 May 2018, 22:04

                        Did you build your plugin for both debug and release ?

                        M Offline
                        M Offline
                        mleoni
                        wrote on 15 May 2018, 13:08 last edited by
                        #11

                        @SGaist I built just one, with the default configuration. I will check which one when I get back to my home pc. What difference does it make? Could it work with one but not the other one?

                        @Homer2000 I am using a shared library, except my distribution [like many others] now uses MariaDB as a replacement for MySQL, so I link to libmariadb.so or something similar

                        H 1 Reply Last reply 15 May 2018, 14:24
                        0
                        • M mleoni
                          15 May 2018, 13:08

                          @SGaist I built just one, with the default configuration. I will check which one when I get back to my home pc. What difference does it make? Could it work with one but not the other one?

                          @Homer2000 I am using a shared library, except my distribution [like many others] now uses MariaDB as a replacement for MySQL, so I link to libmariadb.so or something similar

                          H Offline
                          H Offline
                          Homer2000
                          wrote on 15 May 2018, 14:24 last edited by
                          #12

                          @mleoni said in Cannot connect to databases, Driver not loaded error.:

                          MariaDB

                          MariaDB use libqsqlmysql plugin?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 15 May 2018, 20:00 last edited by
                            #13

                            @Homer2000 No, by default the Qt MySQL plugin links against the MySQL client libraries.

                            You can build it to use MariaDB though.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              mleoni
                              wrote on 19 May 2018, 09:54 last edited by
                              #14

                              Ok, I found out what I was doing wrong.
                              The code that I was using is

                              QSqlDatabase db;
                              db.addDatabase("QMYSQL");
                              

                              and it was giving me the error above, whereas the correct way to do it is

                              QSqlDatabase db;
                              db = QSqlDatabase::addDatabase("QMYSQL");
                              

                              and with this code it works fine.

                              I don't know why this is the case, I guess today I just learnt something new on Qt, thanks for the help everyboody!

                              1 Reply Last reply
                              1
                              • C Offline
                                C Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 19 May 2018, 13:32 last edited by
                                #15

                                @mleoni said in Cannot connect to databases, Driver not loaded error.:

                                I don't know why this is the case,

                                Because QSqlDatabase::addDatabase() is a static function - no Qt magic here.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                1
                                • M Offline
                                  M Offline
                                  mleoni
                                  wrote on 19 May 2018, 19:57 last edited by
                                  #16

                                  I see, so my mistake was letting the returned value go lost. Thanks for clarifying this!

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 19 May 2018, 20:02 last edited by
                                    #17

                                    Please take the time to read the QSqlDatabase documentation. It shows how to use it properly.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mleoni
                                      wrote on 19 May 2018, 20:33 last edited by
                                      #18

                                      Thanks for the reference!

                                      1 Reply Last reply
                                      0

                                      1/18

                                      13 May 2018, 18:04

                                      • Login

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