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. [SOLVED]QSqlDatabase

[SOLVED]QSqlDatabase

Scheduled Pinned Locked Moved General and Desktop
24 Posts 5 Posters 12.0k 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.
  • F Offline
    F Offline
    FreeQy
    wrote on last edited by
    #1

    Hi guys,
    i'm just freaking out trying to connect to a database with my qt.
    First of all, I run the QT Creator 2.7.0 (based on QT 5.0.2) on Windows 8.
    I tried to look through the plugins installed with QT, but I didn't noticed anything related to database, mysql or whatever.

    So, I have my method:

    @void MainWindow::connectDB()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("postgres");
    db.setUserName("postgres");
    db.setPassword("password");
    bool ok = db.open();
    }@

    and I also included the
    @#include <QtSql/QSqlDatabase>@

    and when i try to call my method, i get a lot of errors like this:
    !http://i49.tinypic.com/25k6b2v.jpg(error)!

    I'll copy you just the first:
    @mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSqlDatabase::~QSqlDatabase(void)" (_imp??1QSqlDatabase@@QEAA@XZ) referenced in function "private: void __cdecl MainWindow::connectDB(void)" (?connectDB@MainWindow@@AEAAXXZ)@

    Does someone know how to deal with that?
    Thanks a lot

    Cheers,
    Nick

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      I believe (not sure though) after skimming through the modules, that you need to add QT += sql to your PRO file.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to DevNet,

        Did you add QT += sql in your pro file ?

        On a related note, did you build the "MySql":http://qt-project.org/doc/qt-5.0/qtsql/sql-driver.html#how-to-build-the-qmysql-plugin-on-windows 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
        • F Offline
          F Offline
          FreeQy
          wrote on last edited by
          #4

          Forgot to mention that I added the sql to the QT += core gui so it became
          @QT += core gui
          sql@

          1 Reply Last reply
          0
          • F Offline
            F Offline
            FreeQy
            wrote on last edited by
            #5

            [quote author="SGaist" date="1366208236"]Hi and welcome to DevNet,

            Did you add QT += sql in your pro file ?

            On a related note, did you build the "MySql":http://qt-project.org/doc/qt-5.0/qtsql/sql-driver.html#how-to-build-the-qmysql-plugin-on-windows plugin ?[/quote]

            Hi and thanks :D

            about building the MySql...i experience a lot of troubles in doing that...
            the terminal doesn't even take in count the 'qmake' command
            but if it's the solution for my problems, than I have to figure out how to build it haha

            1 Reply Last reply
            0
            • rincewindR Offline
              rincewindR Offline
              rincewind
              wrote on last edited by
              #6

              BTW, it's Qt not QT (Quick Time).

              1 Reply Last reply
              0
              • F Offline
                F Offline
                FreeQy
                wrote on last edited by
                #7

                !http://i49.tinypic.com/71srkl.jpg(version)!

                i'm using the 64bit of Qt....can this be the problem? :S

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  For the qmake problem, simply provide the complete path to qmake when building 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
                  • F Offline
                    F Offline
                    FreeQy
                    wrote on last edited by
                    #9

                    [quote author="SGaist" date="1366211235"]For the qmake problem, simply provide the complete path to qmake when building the plugin[/quote]

                    The thing is that i cannot find the plugin that is written here:
                    @cd %QTDIR%\src\plugins\sqldrivers\mysql
                    qmake "INCLUDEPATH+=C:/MySQL/include" "LIBS+=C:/MYSQL/MySQL Server <version>/lib/opt/libmysql.lib" mysql.pro
                    nmake@

                    i mean the 'mysql' in the sqldrivers folder...

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Get the source package then, you'll find what you need in there

                      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
                      • F Offline
                        F Offline
                        FreeQy
                        wrote on last edited by
                        #11

                        Could you provide me the right link? to know for sure that what i'm downloading is the right package :D

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          panosk
                          wrote on last edited by
                          #12

                          Hm, aren't you actually trying to connect to PostgreSQL and not MySQL? :)

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            FreeQy
                            wrote on last edited by
                            #13

                            well, for me it would be really the same if connecting postresql or mysql.
                            I was trying to make one of them work, but I couldn't find the way...

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              panosk
                              wrote on last edited by
                              #14

                              OK, got confused cause your sample code tries to connect to Postrgre and then all the discussion was about MySQL. As SGaist suggested, you need the sources, but don't know where you can get them as I use the SDK which includes the sqldriver sources.

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                FreeQy
                                wrote on last edited by
                                #15

                                Can you than maybe link to me where to download the SDK from?
                                'cause today i tried 3-4 different Qts....and nothing :S

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  panosk
                                  wrote on last edited by
                                  #16

                                  Well, at the top of the download page you can choose a Windows SDK, nothing confusing there. However, are you sure you are searching in the right place for the mysql driver? If you already have an SDK installation, the full path to the MySQL driver folder is this
                                  @
                                  QtRootInstallationFolder/Src/qtbase/src/plugins/sqldrivers/mysql
                                  @

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    FreeQy
                                    wrote on last edited by
                                    #17

                                    haha, that's nice :D
                                    ok, the files are there, so i just have to compile them with the 'qmake' through my windows terminal?
                                    compiled it (i saw that with the normal terminal the qmake command didn't work, so i did it with the terminal provided by Qt), but now the final make command gives me an error lol

                                    @C:\Qt\Qt5.0.2\5.0.2\Src\qtbase\src\plugins\sqldrivers\mysql>make
                                    make -f Makefile.Release all
                                    make[1]: Entering directory /c/Qt/Qt5.0.2/5.0.2/Src/qtbase/src/plugins/sqldrive rs/mysql' g++ -c -pipe -fno-keep-inline-dllexport -O2 -std=c++0x -fno-exceptions -frtti -W all -Wextra -DUNICODE -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCE PTIONS -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I. -I'/usr/local/in clude' -I'C:/Qt/Qt5.0.2/5.0.2/mingw47_32/include' -I'C:/Qt/Qt5.0.2/5.0.2/mingw47 _32/include/QtSql' -I'C:/Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtSql/5.0.2' -I'C:/ Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtSql/5.0.2/QtSql' -I'C:/Qt/Qt5.0.2/5.0.2/mi ngw47_32/include/QtCore' -I'.moc/release_shared' -I'../../../../mkspecs/win32-g+ +' -o .obj/release_shared/main.o main.cpp In file included from main.cpp:44:0: ../../../sql/drivers/mysql/qsql_mysql.h:52:19: fatal error: mysql.h: No such fil e or directory compilation terminated. make[1]: *** [.obj/release_shared/main.o] Error 1 make[1]: Leaving directory /c/Qt/Qt5.0.2/5.0.2/Src/qtbase/src/plugins/sqldriver
                                    s/mysql'
                                    make: *** [release-all] Error 2
                                    @

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      panosk
                                      wrote on last edited by
                                      #18

                                      OK, one step closer :). I don't quite get what you mean with the terminal issue, but are you giving the path to the mysql include folder in your qmake command, as you wrote in a previous post?
                                      @
                                      "INCLUDEPATH+=C:/MySQL/include"
                                      @
                                      The mysql.h file should be in there.

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        FreeQy
                                        wrote on last edited by
                                        #19

                                        Well, there was no MySQL folder
                                        i just downloaded a lot of packages, and so i will now retry :D
                                        thanks

                                        1 Reply Last reply
                                        0
                                        • F Offline
                                          F Offline
                                          FreeQy
                                          wrote on last edited by
                                          #20

                                          OK

                                          everything done, compiling with both qmake and make, and no errors came out :D

                                          now, going back to Qt:

                                          i've put one line of code to try the DB:
                                          @void MainWindow::connectDB()
                                          {
                                          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                                          }
                                          @

                                          and the output is just:

                                          @C:\Users\Nick\Documents\QT_Projects\HotelManagement\mainwindow.cpp:39: error: undefined reference to `_imp___ZN12QSqlDatabase17defaultConnectionE'@

                                          is it something that i just don't get?

                                          EDIT: !http://i46.tinypic.com/14v4fuo.jpg(code)!

                                          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