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. QSql classes architecture
Qt 6.11 is out! See what's new in the release blog

QSql classes architecture

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 4.3k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    You should take a look a QDataWidgetMapper

    That will simplify your code.

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

    K 1 Reply Last reply
    1
    • SGaistS SGaist

      You should take a look a QDataWidgetMapper

      That will simplify your code.

      K Offline
      K Offline
      Koukoumaxe
      wrote on last edited by
      #6

      @SGaist Wow ! that indeed will simplify my code ! From the first look, i can replace my set/get methods with these models right ?

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

        Yes, the Book Demonstration Example shows you how to use it.

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

        K 1 Reply Last reply
        1
        • SGaistS SGaist

          Yes, the Book Demonstration Example shows you how to use it.

          K Offline
          K Offline
          Koukoumaxe
          wrote on last edited by
          #8

          @SGaist Thank you very much.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Koukoumaxe
            wrote on last edited by Koukoumaxe
            #9

            Do you think it will be a bad practice to use different class for each table ?
            Since each table has different columns and thus different queries.

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #10

              How do you want to handle queries which affects more than one table with such approach?
              I would not use one class for each table, because then your design is affected by the number of tables in the database.
              What if you later add one table and remove two? You would need to write one new class and remove 2.
              I would use one class and implement methods to do specific tasks. This way in your code which uses this class you do not have to know how the data is stored (in which tables).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply
              0
              • jsulmJ jsulm

                How do you want to handle queries which affects more than one table with such approach?
                I would not use one class for each table, because then your design is affected by the number of tables in the database.
                What if you later add one table and remove two? You would need to write one new class and remove 2.
                I would use one class and implement methods to do specific tasks. This way in your code which uses this class you do not have to know how the data is stored (in which tables).

                K Offline
                K Offline
                Koukoumaxe
                wrote on last edited by
                #11

                @jsulm I generaly use Inner join for queries affecting more than one table.
                When it comes to Inserting and Updating, i don't want the user to be able to add new data for an instrument for example, from a form about instrument methods.

                In the scenario you gave, i would still have to write a couple of methods for each of the tables and delete the unused method right ? I mean each table will still need methods for the queries.

                Also what about security ? I am not really sure, but isn't easier for someone to have access to an admin table data (when he shouldn't), when these data are stored in a class that he already has access in ?

                1 Reply Last reply
                0
                • jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  I don't understand why you want to write something specific for each and every table?
                  The methods I mentioned should have generic names like: void addSomething(const Something&).
                  "Something" could be stored in one or even more tables, the caller does simply not care about such details.
                  So, the interface should not reflect your database design.

                  It is not clear to me what you mean with security? The interface should not provide access to data which should be hidden and it does not matter how the interface is designed (whether you use one class per table or not). And who is "someone"? A developer using your interface or a real application user?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Koukoumaxe
                    wrote on last edited by Koukoumaxe
                    #13

                    I currently have a method to handle each query.
                    So instead you suggest having something like this to be used for all the tables ?

                    void addSomething(QString queryString){
                             query.exec(queryString);
                    

                    And for SELECT queries i should use your solution with QDataWidgetMapper and QModel ?

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      What I suggest is: decouple your interface from the database design. You can have a method for one query, you can have a method which executes several queries,... SQL database is an implementation detail and should not influence the interface too much.
                      The user of the interface should not care about databases and how they are designed. They just use the interface to do what ever needs to be done. Ideally it should be possible to replace your SQL database with, for example, files or a non SQL database without changing the interface, or to change the database design without changing the interface (or with minor changes).
                      Example: if you use QPushButton you don't care how a button is implemented on Windows, MacOS, Linux,... For you it is just a button.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • K Offline
                        K Offline
                        Koukoumaxe
                        wrote on last edited by
                        #15

                        @jsulm I see ! It is clear what you suggest.
                        I think indeed this is a better way to proceed .
                        Thank you for your time !

                        1 Reply Last reply
                        0
                        • jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          You're welcome!

                          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