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.
  • 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