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. Showing data from multiple SQLite tables
QtWS25 Last Chance

Showing data from multiple SQLite tables

Scheduled Pinned Locked Moved Solved General and Desktop
databasemodel-viewsqlite view
7 Posts 2 Posters 2.5k Views
  • 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
    Pl45m4
    wrote on 22 Apr 2019, 13:45 last edited by Pl45m4
    #1

    I have a SQLite database with 6 tables (some of them with two foreign keys). What would be the easiest solution to create a view to display data (join on multiple tables)?
    I found out, that the QSqlRelationalTableModel works well, but only with one table / one join.
    QSqlQueryModel and QSqlTableModel both dont provide the foreign key support with delegates.

    Is there a way to keep the foreign key support with the possibility of joining data from multiple tables together with simple insertation? How to insert / alter / delete data? Do I have to write querys for every single change?


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 22 Apr 2019, 17:43 last edited by
      #2

      Just put your query with the joins into a QSqlQueryModel

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply 23 Apr 2019, 00:07
      4
      • C Christian Ehrlicher
        22 Apr 2019, 17:43

        Just put your query with the joins into a QSqlQueryModel

        P Offline
        P Offline
        Pl45m4
        wrote on 23 Apr 2019, 00:07 last edited by Pl45m4
        #3

        @Christian-Ehrlicher

        SqlQueryModel works with Delegates and Relations?
        I dont want to write thousand lines of code just for the querys.
        And I guess you can not change a value there by clicking into the model and start typing?

        From QSqlQueryModel Doc:
        The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

        So I have to code every functionality that e.g. the QSqlRelationalTableModel provides?
        That's a lot of work....


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 23 Apr 2019, 06:09 last edited by
          #4

          I don't understand what you mean - you said you want to display data from different tables. So what's wrong writing a query with a join on all columns you want to display and pass t to a QSqlQueryModel?
          Editing such data will not work with any of the convenience QSql*Models in your case - you have to write your own model then.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply 23 Apr 2019, 10:54
          3
          • C Christian Ehrlicher
            23 Apr 2019, 06:09

            I don't understand what you mean - you said you want to display data from different tables. So what's wrong writing a query with a join on all columns you want to display and pass t to a QSqlQueryModel?
            Editing such data will not work with any of the convenience QSql*Models in your case - you have to write your own model then.

            P Offline
            P Offline
            Pl45m4
            wrote on 23 Apr 2019, 10:54 last edited by Pl45m4
            #5

            @Christian-Ehrlicher

            Ok, didn't know that... Thought all Models are editable (Views obviously not)

            Edit: I can edit when I'm using a QSqlRelationalTableModel... this comes very close to what I want / need, but it only works on one table :(


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 23 Apr 2019, 11:03 last edited by
              #6

              This is what I said - how should the model be able to update data in different tables when they are all joined? This is a task which can't be done generally (at least not for all cases). You have to do it on your own.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              P 1 Reply Last reply 23 Apr 2019, 12:23
              4
              • C Christian Ehrlicher
                23 Apr 2019, 11:03

                This is what I said - how should the model be able to update data in different tables when they are all joined? This is a task which can't be done generally (at least not for all cases). You have to do it on your own.

                P Offline
                P Offline
                Pl45m4
                wrote on 23 Apr 2019, 12:23 last edited by
                #7

                @Christian-Ehrlicher

                Hm ok... That's not what I wanted to hear :D
                Thank you anyway.
                I think, I will subclass QSqlQueryModel then and write functions to make the model writable...


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0

                1/7

                22 Apr 2019, 13:45

                • Login

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