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. Basic qt with mysql?
Forum Updated to NodeBB v4.3 + New Features

Basic qt with mysql?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 8.6k 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.
  • D Offline
    D Offline
    doforumda
    wrote on 10 Oct 2010, 10:45 last edited by
    #1

    hi

    i install qt creator then mysql driver for qt and then i install xampp to make databases for qt. Now i want to access data in database using qt. i am following this link but it is not working for me. "Your text to link here...":http://doc.trolltech.com/3.0/sql.html

    then problem is when i type the same code
    @
    #include <QApplication>
    #include <QSqlDatabase>

    int main( int argc, char *argv[] )
    {
        QApplication app( argc, argv );
    
        QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( QMYSQL );
        if ( ! defaultDB ) {
            qWarning( "Failed to connect to the database driver" );
            return 1;
        }
        defaultDB->setDatabaseName( "testTwo" );
        defaultDB->setUserName( "root" );
        defaultDB->setPassword( "" );
        defaultDB->setHostName( "localhost" );
    
        if ( defaultDB->open() ) {
            // Database successfully opened; we can now issue SQL commands.
        }
    
        return 0;
    }
    

    @

    then it returns these errors
    @
    QSqlDatabase: No such file or directory main.cpp 2
    QSqlDatabase was not declare in this scope main.cpp 9
    defaultDB was not declare in this scope main.cpp 9
    QSqlDatabase is not a class or namespace main.cpp 9
    QMYSQL was not declare in this scope main.cpp 9
    @

    how can i access data from my database table?
    other thing is i am using xampp for creating mysql databases. when i make database it is placed in this directory opt/lampp/var/mysql/testTwo.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on 10 Oct 2010, 10:49 last edited by
      #2

      in .pro file you should add
      @QT += sql@

      --
      Vasiliy

      1 Reply Last reply
      0
      • D Offline
        D Offline
        doforumda
        wrote on 10 Oct 2010, 10:56 last edited by
        #3

        now it displays this error
        @
        in function int main(int, char**)
        cannot convert to QSqlDatabase to QSqlDatabase in inititalization main.cpp 9
        @

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zlatomir
          wrote on 10 Oct 2010, 11:26 last edited by
          #4

          That's because you assign QSqlDatabase *defaultDB ( a pointer) with what "addDatabase(...)":http://doc.qt.nokia.com/4.6/qsqldatabase.html#addDatabase returns (a QSqlDatabase, not a pointer to QSqlDatabase)

          https://forum.qt.io/category/41/romanian

          1 Reply Last reply
          0
          • D Offline
            D Offline
            doforumda
            wrote on 10 Oct 2010, 11:29 last edited by
            #5

            so i should use something like this QSqlDatabase defaultDB = QSqlDatabase::addDatabase("QMYSQL");

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on 10 Oct 2010, 11:30 last edited by
              #6

              Yes. And in case of one db without name (i.e. default database) you don't need to provide it to QSqlQuery.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                doforumda
                wrote on 10 Oct 2010, 11:34 last edited by
                #7

                now it displays this error
                @
                in function int main(int, chat**)
                no match for operator! in !db
                candidates are operator!(bool) <built-in>
                @

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  Zlatomir
                  wrote on 10 Oct 2010, 11:54 last edited by
                  #8

                  You can use "isValid()":http://doc.qt.nokia.com/4.6/qsqldatabase.html#isValid

                  https://forum.qt.io/category/41/romanian

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    doforumda
                    wrote on 10 Oct 2010, 11:58 last edited by
                    #9

                    now it is saying
                    @
                    QSqlDatabase: QMysql driver is not loaded
                    QSqlDatabase: available drivers :QSQLITE QSQLITE2
                    @

                    i have installed Mysql driver here "Your text to link here...":http://developer.qt.nokia.com/forums/viewthread/1121/P30/

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DenisKormalev
                      wrote on 10 Oct 2010, 16:23 last edited by
                      #10

                      It says that something is wrong with your mysql plugin. What do you have in your plugins/sqldrivers folder?

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        doforumda
                        wrote on 10 Oct 2010, 16:35 last edited by
                        #11

                        i have 10 different folders and four files. files with names are libqsqlmysql.so, qsqldriverbase.pri, readme, sqldriver.pro

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          doforumda
                          wrote on 10 Oct 2010, 16:37 last edited by
                          #12

                          i also run this code
                          @
                          #include <QCoreApplication>
                          #include <QSqlDatabase>
                          #include <QSqlError>
                          #include <QStringList>
                          #include <QtDebug>

                            int main( int argc, char *argv )
                            {
                            QCoreApplication app( argc, argv );
                            qDebug() << QSqlDatabase::drivers();
                            }
                          

                          @

                          it displays this
                          @
                          ("QSQLITE","QSQLITE2")
                          @

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DenisKormalev
                            wrote on 10 Oct 2010, 17:25 last edited by
                            #13

                            Not folder with plugin sources, but folder with qt plugin binaries. It is folder named plugins/sqldrivers in root of your Qt folder.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              doforumda
                              wrote on 10 Oct 2010, 17:42 last edited by
                              #14

                              well i checked this in this path home/qtsdk-2010.05/qt/src/plugins/sqldrivers

                              I have two plugins folders. one in home/qtsdk-2010.05/qt/plugins
                              in this plugin folder i have 11 folders with one named sqldrivers. inside this sqldriver folder i have these 3 files "libqsqlite.so, libqsqlite2.so, libqsqlpsql.so"

                              the other is mentioned above which inside src.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                DenisKormalev
                                wrote on 10 Oct 2010, 18:15 last edited by
                                #15

                                Put mysql plugin in this folder.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  doforumda
                                  wrote on 10 Oct 2010, 18:17 last edited by
                                  #16

                                  from which folder i can get mysql plugin and what is the name of that mysql plugin? you mean this one libqsqlmysql.so which is in src/plugins/sqldrivers/mysql folder?

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    DenisKormalev
                                    wrote on 10 Oct 2010, 18:28 last edited by
                                    #17

                                    yes

                                    1 Reply Last reply
                                    0

                                    3/17

                                    10 Oct 2010, 10:56

                                    14 unread
                                    • Login

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