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. Connection to a MySQL database with Qt Creator on Windows
Forum Updated to NodeBB v4.3 + New Features

Connection to a MySQL database with Qt Creator on Windows

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatordatabasemysql
11 Posts 5 Posters 9.5k Views 1 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.
  • J Offline
    J Offline
    Justin_Ahinon
    wrote on 20 Dec 2017, 17:58 last edited by A Former User
    #1

    Hi everybody.
    I have one problem and I need your help.
    I'm working on a Qt project where I need to connect the software to a database.
    I try this code in my main code source

    bool createConnection()
    {
        QSqlDatabase laBase = QSqlDatabase::addDataBase("QMYSQL");
        laBase.setHostName("localhost");
        laBase.setDataBaseName("test");
        laBase.setUserName("justin");
        laBase.setPassword("******");
    
        if (!laBase.open()){
           QMessageBox:: critical (0 /* ... */ );
           QObject::tr("Error"), 
           laBase.lastError().text());
           return false;
        } 
        return true; 
    }
    

    But when I run it, I have a message box with this characteristics:
    Title of the message box: Error
    Content: Driver not loaded Driver not loaded

    And I have this message error

    QSqlDatabase : QMYSQL driver not loaded
    QSqlDatabase : available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7   
    

    I've a similar error when I use QSQLITE.

    I'm on Windows 10, 64 bits
    With Qt Creator 4.0.2 based on Qt 5.7.0

    I'll be very happy to be helped.

    Thanks.

    [Edit: added code tags ~~ @Wieland]

    Ahinon Sègbédji Justin

    S 1 Reply Last reply 20 Dec 2017, 18:43
    0
    • A Offline
      A Offline
      Advanced
      wrote on 20 Dec 2017, 18:10 last edited by
      #2

      @Justin_Ahinon said in Connection to a MySQL database with Qt Creator on Windows:

      QMYSQL

      Hi
      Did you try with fresh install of Qt?

      J 1 Reply Last reply 20 Dec 2017, 18:17
      0
      • A Advanced
        20 Dec 2017, 18:10

        @Justin_Ahinon said in Connection to a MySQL database with Qt Creator on Windows:

        QMYSQL

        Hi
        Did you try with fresh install of Qt?

        J Offline
        J Offline
        Justin_Ahinon
        wrote on 20 Dec 2017, 18:17 last edited by
        #3

        @Advanced
        No.
        But I've uninstalled and reinstalled Qt.

        This night, I'll install the new Qt 5.10 and try again.

        Thanks.

        Ahinon Sègbédji Justin

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Advanced
          wrote on 20 Dec 2017, 18:21 last edited by
          #4

          It's probably fixed your issue, but i recommend to you if you want to sure about stability used latest LTS version of Qt that it's 5.9.3

          1 Reply Last reply
          0
          • J Justin_Ahinon
            20 Dec 2017, 17:58

            Hi everybody.
            I have one problem and I need your help.
            I'm working on a Qt project where I need to connect the software to a database.
            I try this code in my main code source

            bool createConnection()
            {
                QSqlDatabase laBase = QSqlDatabase::addDataBase("QMYSQL");
                laBase.setHostName("localhost");
                laBase.setDataBaseName("test");
                laBase.setUserName("justin");
                laBase.setPassword("******");
            
                if (!laBase.open()){
                   QMessageBox:: critical (0 /* ... */ );
                   QObject::tr("Error"), 
                   laBase.lastError().text());
                   return false;
                } 
                return true; 
            }
            

            But when I run it, I have a message box with this characteristics:
            Title of the message box: Error
            Content: Driver not loaded Driver not loaded

            And I have this message error

            QSqlDatabase : QMYSQL driver not loaded
            QSqlDatabase : available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7   
            

            I've a similar error when I use QSQLITE.

            I'm on Windows 10, 64 bits
            With Qt Creator 4.0.2 based on Qt 5.7.0

            I'll be very happy to be helped.

            Thanks.

            [Edit: added code tags ~~ @Wieland]

            S Offline
            S Offline
            Stoyan
            wrote on 20 Dec 2017, 18:43 last edited by
            #5

            @Justin_Ahinon
            If this error occurred also with SQLITE, then check if you have in your *.pro file something like this:

            QT += sql
            
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Dec 2017, 20:54 last edited by
              #6

              Hi,

              To add to my fellow, do you have the MySQL client libraries available on your system ?

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

              J 1 Reply Last reply 20 Dec 2017, 22:57
              1
              • S SGaist
                20 Dec 2017, 20:54

                Hi,

                To add to my fellow, do you have the MySQL client libraries available on your system ?

                J Offline
                J Offline
                Justin_Ahinon
                wrote on 20 Dec 2017, 22:57 last edited by
                #7

                @SGaist
                Yes, I think

                Ahinon Sègbédji Justin

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 21 Dec 2017, 07:54 last edited by
                  #8

                  Run your application with the QT_DEBUG_PLUGINS environment variable set to 1 to see what is happening.

                  You can set that variable in the Run part of the project panel.

                  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
                  • S Offline
                    S Offline
                    Sunfluxgames
                    wrote on 21 Dec 2017, 14:01 last edited by
                    #9

                    I would make sure you complie the mysql plugin..

                    1. make sure you have installed mysql the same lib headers for x86 or x64.
                    2. open mysql plugin located in qbase you will have to comment out a few things as they still havnt fix the bug yet.
                    3. copy those libs and dlls into your complier folder
                    4. copy mysqlclient.dll and qt5sql.dll into your .exe folder
                    5. should be all set...
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Justin_Ahinon
                      wrote on 27 Dec 2017, 17:40 last edited by
                      #10

                      Hi every body,
                      I solve the problem by using the QSQLITE driver which is installed with Qt.

                      /.pro file
                      QT += widgets sql
                      
                      //.cpp file
                      bool createConnection()
                      {
                          QSqlDatabase laBase = QSqlDatabase::addDataBase("QSQLITE");
                          laBase.setHostName("localhost");
                          laBase.setDataBaseName("test");
                          laBase.setUserName("justin");
                          laBase.setPassword("******");
                      
                          if (!laBase.open()){
                             QMessageBox:: critical (0 /* ... */ );
                             QObject::tr("Error"), 
                             laBase.lastError().text());
                             return false;
                          } 
                          return true; 
                      }
                      

                      Ahinon Sègbédji Justin

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 27 Dec 2017, 21:29 last edited by
                        #11

                        Hi,

                        Are you aware of the implications of moving from MySQL to SQLite ?

                        One of which is: you are using a relative path for your SQLite database which won't work if your users install your application in e.g. Program Files as it's a read-only location.

                        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

                        6/11

                        20 Dec 2017, 20:54

                        • Login

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