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. How to use an Object of type QSqlTableModel in another function of the same class?
Forum Updated to NodeBB v4.3 + New Features

How to use an Object of type QSqlTableModel in another function of the same class?

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 2.0k Views 4 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.
  • M mooswitz

    @artwaw @SGaist @JonB

    I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
    https://pastebin.com/e2SVXZVb

    Here I made an error. It was meant so say:
    "In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
    So I can later check for isOpen(). That seems a lot better to me."

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #10

    @mooswitz
    I don't know why you are getting "spam". I wouldn't normally look at pastebin or anything else external.

    You now have

    static QSqlDatabase db;
    

    as a member variable. Which goes against what the docs tell you (not) to do. Get rid of a member variable and use QSqlDatabase::database() static function per the docs.

    M 1 Reply Last reply
    2
    • JonBJ JonB

      @mooswitz
      I don't know why you are getting "spam". I wouldn't normally look at pastebin or anything else external.

      You now have

      static QSqlDatabase db;
      

      as a member variable. Which goes against what the docs tell you (not) to do. Get rid of a member variable and use QSqlDatabase::database() static function per the docs.

      M Offline
      M Offline
      mooswitz
      wrote on last edited by
      #11

      @JonB

      I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D

      I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:

          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
          db.setHostName(DBHOSTNAME);
          db.setDatabaseName(DBNAME);
          db.setUserName(DBUSERNAME);
          db.setPassword(DBPASSWORD);
      

      then I can get that connection always and everywhere inside my project with simply calling

      QSqlDatabase db = QSqlDatabase::database();
      

      how can I make it work with several database connections? Let's say I have two different databases...

      Would i then add them like so: ?

          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1");
          db.setHostName(DBHOSTNAME);
          db.setDatabaseName(DBNAME);
          db.setUserName(DBUSERNAME);
          db.setPassword(DBPASSWORD);
      
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2");
          db.setHostName(DBHOSTNAME);
          db.setDatabaseName(DBNAME);
          db.setUserName(DBUSERNAME);
          db.setPassword(DBPASSWORD);
      
      JonBJ artwawA 2 Replies Last reply
      1
      • M mooswitz

        @JonB

        I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D

        I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:

            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
            db.setHostName(DBHOSTNAME);
            db.setDatabaseName(DBNAME);
            db.setUserName(DBUSERNAME);
            db.setPassword(DBPASSWORD);
        

        then I can get that connection always and everywhere inside my project with simply calling

        QSqlDatabase db = QSqlDatabase::database();
        

        how can I make it work with several database connections? Let's say I have two different databases...

        Would i then add them like so: ?

            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1");
            db.setHostName(DBHOSTNAME);
            db.setDatabaseName(DBNAME);
            db.setUserName(DBUSERNAME);
            db.setPassword(DBPASSWORD);
        
            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2");
            db.setHostName(DBHOSTNAME);
            db.setDatabaseName(DBNAME);
            db.setUserName(DBUSERNAME);
            db.setPassword(DBPASSWORD);
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #12

        @mooswitz
        Look at the docs!
        QSqlDatabase QSqlDatabase::database(const QString &connectionName = QLatin1StringView(defaultConnection), bool open = true)

        Returns the database connection called connectionName.

        1 Reply Last reply
        2
        • M mooswitz

          @JonB

          I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D

          I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:

              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
              db.setHostName(DBHOSTNAME);
              db.setDatabaseName(DBNAME);
              db.setUserName(DBUSERNAME);
              db.setPassword(DBPASSWORD);
          

          then I can get that connection always and everywhere inside my project with simply calling

          QSqlDatabase db = QSqlDatabase::database();
          

          how can I make it work with several database connections? Let's say I have two different databases...

          Would i then add them like so: ?

              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1");
              db.setHostName(DBHOSTNAME);
              db.setDatabaseName(DBNAME);
              db.setUserName(DBUSERNAME);
              db.setPassword(DBPASSWORD);
          
              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2");
              db.setHostName(DBHOSTNAME);
              db.setDatabaseName(DBNAME);
              db.setUserName(DBUSERNAME);
              db.setPassword(DBPASSWORD);
          
          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #13

          @mooswitz More or less yes. But try first with one connection and see how it works, don't be afraid to recall it everywhere you might need to. I'd wager it will work without a snag.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • M mooswitz

            @artwaw @SGaist @JonB

            I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
            https://pastebin.com/e2SVXZVb

            Here I made an error. It was meant so say:
            "In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
            So I can later check for isOpen(). That seems a lot better to me."

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #14

            @mooswitz in addition to what my fellows wrote: the SQL related classes you use offer error methods that allow you to learn what went wrong. Using them will help you more than just printing "something wrong happened".

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

            artwawA 1 Reply Last reply
            1
            • SGaistS SGaist

              @mooswitz in addition to what my fellows wrote: the SQL related classes you use offer error methods that allow you to learn what went wrong. Using them will help you more than just printing "something wrong happened".

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #15

              @SGaist Great suggestion, I forgot to mention them.
              You can #include <QSqlError> and then qDebug() << relevant db and QSqlQuery (and others) .error() or .lastError().text() and similar.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mooswitz
                wrote on last edited by mooswitz
                #16

                @SGaist @artwaw

                Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.

                My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
                I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.

                But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.

                Greetz, mooswitz

                SGaistS 1 Reply Last reply
                0
                • M mooswitz

                  @SGaist @artwaw

                  Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.

                  My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
                  I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.

                  But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.

                  Greetz, mooswitz

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #17

                  @mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:

                  My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
                  I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.

                  Don't, that would be unclean. It is not a design pattern that would fit that use case.

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

                  M 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:

                    My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
                    I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.

                    Don't, that would be unclean. It is not a design pattern that would fit that use case.

                    M Offline
                    M Offline
                    mooswitz
                    wrote on last edited by
                    #18

                    @SGaist said in How to use an Object of type QSqlTableModel in another function of the same class?:

                    Don't, that would be unclean. It is not a design pattern that would fit that use case.

                    Okay. So What would be the better approach? I have taken the time now to figure out what would work better instead... so first: Why is there the "submitAll()" method, if I should'nt use it with a Button or sth. similar to trigger it? Or did you mean that I should not use the member variable for that?

                    What I want to do is the following: You (as a user) get presented with a TableView, showing some Data from a mariadb. This Data (in my case userdata) can be edited here. I do not want it to update directly after change. So that's Why I want a button to trigger that. Regarding the option to use QSqlTableModel for that - what is then the better option to trigger an Insert to the db with the Data loaded with the TableModel?

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

                      The unclean part was about trying to have a global QSqlTableModel in a similar fashion to QSqlDatabase.
                      For the rest, you did thing correctly. You could even simplify a bit by connecting the button directly to the submitAll function. Bad suggestion, you'd lose error handling.

                      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
                      • M Offline
                        M Offline
                        mooswitz
                        wrote on last edited by
                        #20

                        Okay. Thanks a lot!

                        How would I do that? I mean: connecting the button directly to the function?

                        SGaistS 1 Reply Last reply
                        0
                        • M mooswitz

                          Okay. Thanks a lot!

                          How would I do that? I mean: connecting the button directly to the function?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #21

                          @mooswitz sorry, that was a bad suggestion, you would lose the error handling check.

                          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