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. Using database connection
Forum Updated to NodeBB v4.3 + New Features

Using database connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 5.0k Views 2 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have two classes, Additem and dbManager. I created a database connection in dbManager:

    void dbManager::mydbManager(const QString &fileQString)
    {
        db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
        db.setDatabaseName (fileQString);
    
        if(!db.open ())
            {
                qDebug() << "Database connection Friend is not open!";
            }
        else
            {
                qDebug() << "Database connection Friend is open!";
            }
    }
    

    When I run a the program it works, qDebug says the connection is open.
    I tried to use it in Additem like this:

      #include "additem.h"
    #include "ui_additem.h"
    
    Additem::Additem(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Additem)
    {
        ui->setupUi(this);
    
        connection();
    
    void Additem::connection()
    {
        dbManager *mdbManager = new dbManager;
        mdbManager->mydbManager (fileQstring);
    }
    

    None of the functions in Additem can use the connection, all of them says that the database connection is not open. How can I use this connection properly? Thank you.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aliks-os
      wrote on last edited by
      #2

      you do all is correct. In you case, when you get message "Database connection Friend is open!";
      it is mean that connection is established correct and you can continue work with DB, execute other command.

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

        Hi,

        You should print the error rather that just the message saying it didn't opened the connection.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 2 Replies Last reply
        0
        • A Offline
          A Offline
          aliks-os
          wrote on last edited by
          #4

          Please change like below

          void Additem::connection()
          {
              dbManager *mdbManager = new dbManager (fileQstring);
          }
          
          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You should print the error rather that just the message saying it didn't opened the connection.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @SGaist
            I modified the qDebug() line like this:

            qDebug() << "Database connection Friend is not open!" << db.lastError ();
            

            The actual error message is "Driver not loaded". It works in dbManager ( I assume it is also loaded), but clearly not in Additem. Is it possible to use a db connection from an other class like I'm trying or I'm supposed to deal with the db in the same class?

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              You should print the error rather that just the message saying it didn't opened the connection.

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              @SGaist
              When I copy

                  db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
                  db.setDatabaseName (fileQstring);
              

              directly into Additem everything works.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aliks-os
                wrote on last edited by aliks-os
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aliks-os
                  wrote on last edited by
                  #8

                  small correction, please try now

                  void Additem::connection()
                  {
                      dbManager *mdbManager = new dbManager (fileQstring);  
                  }
                  
                  void dbManager::mydbManager(const QString &fileQString)
                  {
                      QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
                      db.setDatabaseName (fileQString);
                  
                      if(!db.open ())
                          {
                              qDebug() << "Database connection Friend is not open!" << db.lastError ();
                          }
                      else
                          {
                              qDebug() << "Database connection Friend is open!";
                          }
                  }
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Can you just share the project somewhere so we can take a look at the complete code ?

                    Have this spread across several files makes it difficult to find what is going wrong.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    G 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Can you just share the project somewhere so we can take a look at the complete code ?

                      Have this spread across several files makes it difficult to find what is going wrong.

                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #10

                      @SGaist
                      Thank you. Here are the files:
                      additem.h
                      dbmanager.h
                      mainwindow.h
                      review.h
                      additem.cpp
                      dbmanager.cpp
                      main.cpp
                      mainwindow.cpp
                      review.cpp

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

                        Do you really need three different connections ?

                        It really looks like you are over-engineering it.

                        Like I already said: you can use different connections but there is no benefit in adding/removing them all the time if there's no need. And there's clearly no need in your case. Just setup the connection(s) once and then retrieve the one you want to use with QSqlDatabase::database.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        G 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Do you really need three different connections ?

                          It really looks like you are over-engineering it.

                          Like I already said: you can use different connections but there is no benefit in adding/removing them all the time if there's no need. And there's clearly no need in your case. Just setup the connection(s) once and then retrieve the one you want to use with QSqlDatabase::database.

                          G Offline
                          G Offline
                          gabor53
                          wrote on last edited by
                          #12

                          @SGaist
                          If I create a connection in dbmanager.cpp like this:

                              db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
                              db.setDatabaseName (fileQString);
                          

                          how can I retrieve this connection in the other classes? When I create an object like

                          dbManager *mdbManager = new dbManager();
                          mdbManager->mydbManager (fileQstring);
                          

                          in additem.cpp I keep getting the message "driver not loaded". What code to use to retrieve the same connection in all the 3 classes (you are right I don't need more than one connection).
                          Thank you.

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

                            The "Driver not loaded" error is pretty surprising unless it happens on a deployed application which has the SQL plugins missing.

                            As for how to retrieve the connection, I've already wrote it: QSqlDatabase::database

                            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
                            1
                            • G Offline
                              G Offline
                              gabor53
                              wrote on last edited by
                              #14

                              It is not a deployed application.

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

                                Then you should add the QT_DEBUG_PLUGINS environment variable to the Run part of the Project panel and set it to 1 to see what is happening with the plugin load.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                G 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  Then you should add the QT_DEBUG_PLUGINS environment variable to the Run part of the Project panel and set it to 1 to see what is happening with the plugin load.

                                  G Offline
                                  G Offline
                                  gabor53
                                  wrote on last edited by
                                  #16

                                  @SGaist
                                  After removing line 606 from additem.cpp

                                  QSqlDatabase::removeDatabase ("Friend");
                                  
                                  

                                  I don't have the the driver not loaded error message.

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

                                    Well indeed, if you remove the database connection and try to access it again without prior setup, the error message makes more sense.

                                    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

                                    • Login

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