Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. I can't connect to my MySQL database [Solved]
Forum Update on Monday, May 27th 2025

I can't connect to my MySQL database [Solved]

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 5 Posters 11.1k 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.
  • B Offline
    B Offline
    BadCombination
    wrote on last edited by
    #1

    Hi, I'm learning to program in Qt, but I can't connect with my database.

    I don't find my error, Qt Creator says the variable type is incomplete.
    !http://i46.tinypic.com/a4su2q.png(Error in MySQL connection)!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Have you included sql at your pro file? Please also make sure that your Qt MySQL driver is installed. To install it on fedora you should run something like:
      @yum install qt-mysql@

      http://anavi.org/

      1 Reply Last reply
      0
      • G Offline
        G Offline
        guziemic
        wrote on last edited by
        #3

        Did you include?

        @
        #include <QSqlDatabase>
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          In theory this is enough:

          @
          #include <QtSql>
          ...
          QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL" );
          db.setHostName( "localhost" );
          db.setDatabaseName( "yadayada" );
          db.setUserName( "yadayada" );
          db.setPassword( "yadayada" );

          if( !db.open() )
          {
          qDebug() << db.lastError();
          qFatal( "Failed to connect." );
          }

          qDebug( "Connected!" );
          ...
          @

          In *.pro file:
          @
          QT += sql
          @

          Anyway your error message is a typical text when you forget some include file, in this case:
          #include <QSqlDatabase>

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BadCombination
            wrote on last edited by
            #5

            Thanks to everyone, I can run with the database including this code in the header of main.cpp.

            @#include <QtSql/QSqlDatabase>@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Macro
              wrote on last edited by
              #6

              Jus try including Qt += sql in the .pro file and try...

              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