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. Database connectivity in qt
Forum Updated to NodeBB v4.3 + New Features

Database connectivity in qt

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 5.4k 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.
  • P Offline
    P Offline
    pranj51
    wrote on last edited by
    #1

    I am a beginner , can anybody please help me out with database connectivity using qt? any basic application will do,like performing
    some operations on two numbers using qt app and storing result in database the preferred database is MS Access .
    looking forward for your reply..

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HuXiKa
      wrote on last edited by
      #2

      "Qt Database Classes":http://doc.qt.nokia.com/stable/database.html

      If you can find faults of spelling in the text above, you can keep them.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Well, I assume you have already read the documentation about the "QtSql":http://doc.qt.nokia.com/stable/qtsql.html module, "SQL Programming":http://doc.qt.nokia.com/stable/sql-programming.html and the "examples":http://doc.qt.nokia.com/stable/examples-sql.html, haven't you?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pranj51
          wrote on last edited by
          #4

          Thank you very much for your response ,i have gone through the examples,i am trying to write a basic program to connect to MS Access database but i am not able to create a instance of QSqlQuery in order to fire a 'Select' query on the database. (i tried including header file 'QSqlQuery')

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            So - what was the error?

            Have you added QT += sql to your .pro file?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pranj51
              wrote on last edited by
              #6

              yes i have already added that.Another doubt that i have is where should the database reside so that the qt app should make use of it?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Did it work after you've added it?

                Don't let us extract every single piece of information out of you. Provide clear, concrete, complete and meaningful "questions":http://www.catb.org/~esr/faqs/smart-questions.html.

                As to your last question: it depends. If the data is application-local feel free to distribute the database along with your application (however I would prefer SQLite over Access in all respects). If it is shared among multiple instances or sites you may need a central database server. However, this is just a rule of thumb!

                But - again - there is no way we can give you a reasonable answer with this little amount of information you provide.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pranj51
                  wrote on last edited by
                  #8

                  I apologies for not being clear about the doubts I have. I really Appreciate your immediate response to my qurey, Now I'm able to create QSqlQuery Instance.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pranj51
                    wrote on last edited by
                    #9

                    hii,
                    i have been trying to develop an app to connect to database using "Sqlite" i have tried just to establish connection with database.
                    Program its throwing many errors i'm listing them out it would be great if you have can guide me through this.

                    1)error: QtSql/private/qsqlcachedresult_p.h: No such file or directory
                    2) error: 'QSqlCachedResult' has not been declared

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pranj51
                      wrote on last edited by
                      #10

                      Following is the class file where i have written the function "connect_db()" to establish the connection.

                      @
                      #include "db01.h"
                      #include "ui_db01.h"
                      #include"QtSql/qsqldatabase.h"
                      #include"QtSql/qsql.h"
                      #include"QtSql/QSQLiteDriver"
                      #include"QtSql/qsqlquery.h"
                      db01::db01(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::db01)
                      {
                      ui->setupUi(this);
                      connect_db();
                      }

                      db01::~db01()
                      {
                      delete ui;
                      }

                      void db01::connect_db()
                      {
                      QSqlDatabase dbs=QSqlDatabase::addDatabase("QSQLiteDriver");
                      QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
                      db.setDatabaseName( "testdatabase.db" );
                      if( !db.open() )
                      {
                      qDebug() << db.lastError();
                      qFatal( "Failed to connect." );
                      }
                      }
                      @

                      Edit: Enclosed code section in @ tags. Please do that yourself next time; Andre

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        This does not work and is most likely spitting out errors:

                        @
                        QSqlDatabase dbs=QSqlDatabase::addDatabase("QSQLiteDriver");
                        @

                        http://www.catb.org/~esr/faqs/smart-questions.html

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

                          Hii volker,
                          I Removed the line of code that you mentioned and the program is now executing without any errors.
                          thanks alot

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            Good to know it works now.

                            You should be aware, that the first argument to addDatabase is always the driver type (i.e. on of "QSQLITE" or "QMYSQL" for example). If you want to give your database connection a "handle name", add it as a second argument. But IMHO that's only needed in case you have to deal with multiple connections. If you only have one connection, the default scheme should be sufficient.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            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