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] Problem with including QtSql

[Solved] Problem with including QtSql

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 29.8k 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.
  • D Offline
    D Offline
    doforumda
    wrote on last edited by
    #1

    hi
    here is my problem. I am trying to connect with db. i already did that in other example with the help of some users here on this forum which works perfectly fine.
    here is code which does not work
    @
    #include <QApplication>
    #include <QtSql>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("testTwo");
    db.setUserName("root");
    db.setPassword("xxxxxx");
    
    bool ok = db.open();
    
    return 0;
    

    }
    @

    it produces this error
    @
    QtSql: No such file or directory main.cpp:2
    ‘QSqlDatabase’ was not declared in this scope main.cpp:8
    expected ‘;’ before ‘db’ main.cpp:8
    ‘db’ was not declared in this scope main.cpp:9
    @

    while the previous example i did is working fine. I include the same QtSql file and working but in this example it is giving this error. I also added this line
    QT += sql
    to .pro file
    whats is the problem here? please help

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stukdev
      wrote on last edited by
      #2

      maybe #include <QSqlDatabase> ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        doforumda
        wrote on last edited by
        #3

        same error after replacing QtSql with QSqlDatabase

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stukdev
          wrote on last edited by
          #4

          error is QSqlDatabase No such file or directory?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            doforumda
            wrote on last edited by
            #5

            yes it is the error QSqlDatabase No such file or directory

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stukdev
              wrote on last edited by
              #6

              Maybe is a linker problem, are you sure you have correctly installed qt?
              And use correctly library? In this case i think you want build for 4.7 desktop right?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                doforumda
                wrote on last edited by
                #7

                yes right but my other example is working fine
                here it is
                @
                #include <QApplication>
                #include <QtSql>

                int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);

                QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                db.setHostName("localhost");
                db.setDatabaseName("testTwo");
                db.setUserName("root");
                db.setPassword("xxxxxx");
                
                bool ok = db.open();
                //qDebug() << db.lastError().text();
                //qDebug() << ok;
                
                QSqlQuery query(db);
                query.exec&#40;"SELECT * FROM testTable"&#41;;
                while(query.next()) {
                    QString firstName = query.value(0).toString();
                    QString secondName = query.value(1).toString();
                    //int id = query.value(2).toInt();
                    qDebug() << "First Name: " << firstName << "Last Name:" << secondName << "\n";
                    //qDebug() << db.lastError();
                }
                return 0;
                

                }

                @

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  your pro files are similar? try clean build?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    doforumda
                    wrote on last edited by
                    #9

                    you mean i should close my qt creator and open again write the code from start?
                    [quote author="chetankjain" date="1286900346"]your pro files are similar? try clean build?[/quote]

                    1 Reply Last reply
                    0
                    • ? This user is from outside of this forum
                      ? This user is from outside of this forum
                      Guest
                      wrote on last edited by
                      #10

                      I meant, you have one project with working Qt sql code, and another failing to find the includes, so just compare the .pro files and see if they are similar.

                      Next, in Qt Creator, go to Build menu and select "Clean Project" option and then rebuild ... sometimes this could help ...

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        doforumda
                        wrote on last edited by
                        #11

                        i also did that clean project from build menu but still the same problem

                        i think both pro files are different
                        this .pro is for that example which is working
                        @
                        #-------------------------------------------------

                        Project created by QtCreator 2010-10-10T13:05:29

                        #-------------------------------------------------

                        QT += core gui

                        QT += sql

                        TARGET = dbExample
                        TEMPLATE = app

                        SOURCES += main.cpp
                        db.cpp

                        HEADERS += db.h
                        connection.h

                        FORMS += db.ui

                        @

                        and this is the one which is not working
                        @
                        #-------------------------------------------------

                        Project created by QtCreator 2010-10-12T01:37:23

                        #-------------------------------------------------

                        QT += core gui
                        QT += sql;

                        TARGET = untitled
                        TEMPLATE = app

                        SOURCES += main.cpp
                        mainwindow.cpp

                        HEADERS += mainwindow.h

                        FORMS += mainwindow.ui

                        @

                        1 Reply Last reply
                        0
                        • ? This user is from outside of this forum
                          ? This user is from outside of this forum
                          Guest
                          wrote on last edited by
                          #12

                          The second one has a semi colon after SQL, try removing that.
                          You could use this directly
                          @
                          QT += core gui sql
                          @
                          better to have one definition instead of multiple, just easier to maintain

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            doforumda
                            wrote on last edited by
                            #13

                            yup your are right its working now

                            1 Reply Last reply
                            0
                            • ? This user is from outside of this forum
                              ? This user is from outside of this forum
                              Guest
                              wrote on last edited by
                              #14

                              good to hear that

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                stukdev
                                wrote on last edited by
                                #15

                                Yes, but maybe you have to read some qt doc :P

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  pcscrack
                                  Banned
                                  wrote on last edited by
                                  #16
                                  This post is deleted!
                                  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