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. Getting the SQL string which defines a view using C++ QSql
QtWS25 Last Chance

Getting the SQL string which defines a view using C++ QSql

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqlviewms-access
13 Posts 4 Posters 1.3k 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
    PMime
    wrote on 5 Jul 2021, 13:15 last edited by
    #1

    I am just using QSql with C++ to work with a MS Access database. This works so far.

    The problem is that i can not get the original SQL string when I have created a view/query. The Access table MSysQueries contains some information but is somehow cryptic.

    Code snippets are welcome, wether QSql C++ or pure SQL.

    Thank you in advance!

    A 1 Reply Last reply 5 Jul 2021, 13:37
    0
    • P PMime
      5 Jul 2021, 13:15

      I am just using QSql with C++ to work with a MS Access database. This works so far.

      The problem is that i can not get the original SQL string when I have created a view/query. The Access table MSysQueries contains some information but is somehow cryptic.

      Code snippets are welcome, wether QSql C++ or pure SQL.

      Thank you in advance!

      A Offline
      A Offline
      artwaw
      wrote on 5 Jul 2021, 13:37 last edited by
      #2

      @PMime How do you construct the queries? Please show your code.

      For more information please re-read.

      Kind Regards,
      Artur

      P 1 Reply Last reply 5 Jul 2021, 13:48
      0
      • A artwaw
        5 Jul 2021, 13:37

        @PMime How do you construct the queries? Please show your code.

        P Offline
        P Offline
        PMime
        wrote on 5 Jul 2021, 13:48 last edited by
        #3

        @artwaw Here ist an (experimetal) code snippet:

        QSqlQuery oQuery(m_oQtDb);
        bool bSuccess = oQuery.exec("DROP TABLE FreeSQLView");
        if (!bSuccess)
        	qDebug() << oQuery.lastError();
        bSuccess = oQuery.exec("CREATE VIEW FreeSQLView AS SELECT * FROM User");
        if (!bSuccess)
        	qDebug() << oQuery.lastError();
        qDebug() << m_oQtDb.tables(QSql::Views);
        
        A 1 Reply Last reply 5 Jul 2021, 14:39
        0
        • P PMime
          5 Jul 2021, 13:48

          @artwaw Here ist an (experimetal) code snippet:

          QSqlQuery oQuery(m_oQtDb);
          bool bSuccess = oQuery.exec("DROP TABLE FreeSQLView");
          if (!bSuccess)
          	qDebug() << oQuery.lastError();
          bSuccess = oQuery.exec("CREATE VIEW FreeSQLView AS SELECT * FROM User");
          if (!bSuccess)
          	qDebug() << oQuery.lastError();
          qDebug() << m_oQtDb.tables(QSql::Views);
          
          A Offline
          A Offline
          artwaw
          wrote on 5 Jul 2021, 14:39 last edited by
          #4

          @PMime So in order to get last query from QSqlQuery one should use QSqlQuery::lastQuery() - what does that return for you?

          For more information please re-read.

          Kind Regards,
          Artur

          P 1 Reply Last reply 5 Jul 2021, 14:55
          0
          • A artwaw
            5 Jul 2021, 14:39

            @PMime So in order to get last query from QSqlQuery one should use QSqlQuery::lastQuery() - what does that return for you?

            P Offline
            P Offline
            PMime
            wrote on 5 Jul 2021, 14:55 last edited by
            #5

            @artwaw When I use lastQuery() in my code snippet, the output of this function is:

            DROP TABLE FreeSQLView
            CREATE VIEW FreeSQLView AS SELECT * FROM User
            
            A 1 Reply Last reply 5 Jul 2021, 14:59
            0
            • P PMime
              5 Jul 2021, 14:55

              @artwaw When I use lastQuery() in my code snippet, the output of this function is:

              DROP TABLE FreeSQLView
              CREATE VIEW FreeSQLView AS SELECT * FROM User
              
              A Offline
              A Offline
              artwaw
              wrote on 5 Jul 2021, 14:59 last edited by
              #6

              @PMime Isn't this what you were asking for? Or have I misunderstood your problem?

              For more information please re-read.

              Kind Regards,
              Artur

              P 1 Reply Last reply 5 Jul 2021, 15:12
              0
              • A artwaw
                5 Jul 2021, 14:59

                @PMime Isn't this what you were asking for? Or have I misunderstood your problem?

                P Offline
                P Offline
                PMime
                wrote on 5 Jul 2021, 15:12 last edited by
                #7

                @artwaw I basically want to get the sql string for every view in a database. This database does not have necessarily to be from myself.

                A 1 Reply Last reply 5 Jul 2021, 15:14
                0
                • P PMime
                  5 Jul 2021, 15:12

                  @artwaw I basically want to get the sql string for every view in a database. This database does not have necessarily to be from myself.

                  A Offline
                  A Offline
                  artwaw
                  wrote on 5 Jul 2021, 15:14 last edited by
                  #8

                  @PMime That, I believe, should be accessible via driver. You mentioned documentation in the first post?

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  P 1 Reply Last reply 5 Jul 2021, 15:23
                  0
                  • A artwaw
                    5 Jul 2021, 15:14

                    @PMime That, I believe, should be accessible via driver. You mentioned documentation in the first post?

                    P Offline
                    P Offline
                    PMime
                    wrote on 5 Jul 2021, 15:23 last edited by
                    #9

                    @artwaw There is the system table MSysQueries in MS Access. Unfortunately it is difficult to get the sql string from this table.

                    The following link explains the structure of mSysQueries: [https://stackoverflow.com/questions/17329223/what-does-the-data-in-msysqueries-mean]

                    A 1 Reply Last reply 5 Jul 2021, 15:32
                    0
                    • P PMime
                      5 Jul 2021, 15:23

                      @artwaw There is the system table MSysQueries in MS Access. Unfortunately it is difficult to get the sql string from this table.

                      The following link explains the structure of mSysQueries: [https://stackoverflow.com/questions/17329223/what-does-the-data-in-msysqueries-mean]

                      A Offline
                      A Offline
                      artwaw
                      wrote on 5 Jul 2021, 15:32 last edited by
                      #10

                      @PMime Under that link (and links that follow) it is quite well described how to handle queries towards MSysQueriers - you should be able to do that with QSqlQuery without a problem.

                      Also, I believe MSysQueries is considered system table so call to QSqlDatabase::tables() should take a parameter of QSql::SystemTables().

                      Out of curiosity: does qDebug() << m_oQtDb.tables(QSql::Views); return anything useful for you? I don't have access db to try out myself here.

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      P 1 Reply Last reply 5 Jul 2021, 15:49
                      0
                      • A artwaw
                        5 Jul 2021, 15:32

                        @PMime Under that link (and links that follow) it is quite well described how to handle queries towards MSysQueriers - you should be able to do that with QSqlQuery without a problem.

                        Also, I believe MSysQueries is considered system table so call to QSqlDatabase::tables() should take a parameter of QSql::SystemTables().

                        Out of curiosity: does qDebug() << m_oQtDb.tables(QSql::Views); return anything useful for you? I don't have access db to try out myself here.

                        P Offline
                        P Offline
                        PMime
                        wrote on 5 Jul 2021, 15:49 last edited by
                        #11

                        @artwaw I use qDebug() << m_oQtDb.tables(QSql::Views); because I want to test if the list of views is complete. By views I mean a virtual table based on the result set of an SQL statement.

                        It is that SQL statement I want to obtain.

                        Other database systems like Oracle have system tables too. But they present the SQL statement much more easier, see SYS.VIEWS: https://docs.oracle.com/database/timesten-18.1/TTSYS/systemtables.htm#TTSYS390

                        In MSysQueries you have to build the sql string by yourself which is complicated, for example when UNION is involved.

                        J 1 Reply Last reply 6 Jul 2021, 05:45
                        0
                        • C Offline
                          C Offline
                          ChrisW67
                          wrote on 6 Jul 2021, 04:20 last edited by
                          #12

                          I think you are expecting to obtain the equivalent of:

                          create view blah as
                          select columns...
                          from table1  a
                          join table2 b
                          on b.a_pk = a.pk
                          where conditions...
                          group by r
                          having stuff
                          order by x, y, z
                          

                          for each view with a single query returning the text. That is not going to happen with Access (based on the information accessible through the the pages you linked).

                          It looks like you need to query the two system tables for the view of interest, ordering by attribute number and "order", and iterate this set building a query as you go. You are correct, this will be difficult to get right.

                          Access does this for you in its UI. Have you considered automating Access to extract the information? It may be possible... I cannot say, having consigned Access to the pits of hell from whence it came.

                          1 Reply Last reply
                          0
                          • P PMime
                            5 Jul 2021, 15:49

                            @artwaw I use qDebug() << m_oQtDb.tables(QSql::Views); because I want to test if the list of views is complete. By views I mean a virtual table based on the result set of an SQL statement.

                            It is that SQL statement I want to obtain.

                            Other database systems like Oracle have system tables too. But they present the SQL statement much more easier, see SYS.VIEWS: https://docs.oracle.com/database/timesten-18.1/TTSYS/systemtables.htm#TTSYS390

                            In MSysQueries you have to build the sql string by yourself which is complicated, for example when UNION is involved.

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 6 Jul 2021, 05:45 last edited by
                            #13

                            @PMime Sounds like "describe" statement is what you need, see https://www.geeksforgeeks.org/sql-describe-statement/

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

                            1 Reply Last reply
                            0

                            10/13

                            5 Jul 2021, 15:32

                            • Login

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