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. Cannot retrieve the SQL query string from QSqlQuery?
Forum Updated to NodeBB v4.3 + New Features

Cannot retrieve the SQL query string from QSqlQuery?

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlquery
6 Posts 4 Posters 5.6k 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.
  • J Offline
    J Offline
    JonB
    wrote on 15 Sept 2017, 15:52 last edited by JonB
    #1

    Given that the outside world has constructed a QSqlQuery("some string"), I need to be able to retrieve the text of the query. However, I cannot find any member of QSqlQuery which returns this, which seems surprising as it's so fundamental? Why no QSqlQuery::query() or similar?

    EDIT:

    Hang on, is that what QString QSqlQuery::lastQuery() const returns? I'm finding the terminology confusing, as I don't see what it's got to do with "last". Also, I need to see the string before it is ever executed, not afterwards if that's what it means....

    1 Reply Last reply
    0
    • A Offline
      A Offline
      artwaw
      wrote on 15 Sept 2017, 15:58 last edited by
      #2

      Hi,
      but there is, look into the documentation http://doc.qt.io/qt-5/qsqlquery.html#lastQuery

      For more information please re-read.

      Kind Regards,
      Artur

      J 1 Reply Last reply 15 Sept 2017, 16:00
      3
      • A artwaw
        15 Sept 2017, 15:58

        Hi,
        but there is, look into the documentation http://doc.qt.io/qt-5/qsqlquery.html#lastQuery

        J Offline
        J Offline
        JonB
        wrote on 15 Sept 2017, 16:00 last edited by
        #3

        @artwaw
        Thanks for your prompt response --- I think it crossed with my Edit above! I'm currently verifying that works, I do find the "last" in the name a bit confusing!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mostefa
          wrote on 15 Sept 2017, 22:02 last edited by
          #4

          Hi ,

          I think that you need to use this method:

          http://doc.qt.io/qt-5/qsqlquery.html#value

          For example if you have the following query

            QSqlQuery query("SELECT id,name,birthday FROM artist");
          

          value(0) will return the id
          value(1) will return the name
          value(2) will return the birthday

            while (query.next()) {//check that you have next result
                QString id = query.value(0).toInt();
                QString  name = query.value(1).toString();
                QString birthday = query.value(2).toString();
            }
          

          I hope this can help you

          J 1 Reply Last reply 18 Sept 2017, 08:57
          0
          • M mostefa
            15 Sept 2017, 22:02

            Hi ,

            I think that you need to use this method:

            http://doc.qt.io/qt-5/qsqlquery.html#value

            For example if you have the following query

              QSqlQuery query("SELECT id,name,birthday FROM artist");
            

            value(0) will return the id
            value(1) will return the name
            value(2) will return the birthday

              while (query.next()) {//check that you have next result
                  QString id = query.value(0).toInt();
                  QString  name = query.value(1).toString();
                  QString birthday = query.value(2).toString();
              }
            

            I hope this can help you

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 Sept 2017, 08:57 last edited by
            #5

            @mostefa This is not the answer to the question as @JNBarchan want to have the query text not its result.
            From your example he wants to get "SELECT id,name,birthday FROM artist".

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

            J 1 Reply Last reply 19 Sept 2017, 07:47
            2
            • J jsulm
              18 Sept 2017, 08:57

              @mostefa This is not the answer to the question as @JNBarchan want to have the query text not its result.
              From your example he wants to get "SELECT id,name,birthday FROM artist".

              J Offline
              J Offline
              JonB
              wrote on 19 Sept 2017, 07:47 last edited by
              #6

              @jsulm said in Cannot retrieve the SQL query string from QSqlQuery?:

              @mostefa This is not the answer to the question as @JNBarchan want to have the query text not its result.
              From your example he wants to get "SELECT id,name,birthday FROM artist".

              @mostefa As @jsulm says, I am indeed not looking to execute the query, rather only to retrieve its text.
              This has indeed turned out to be QSqlQuery::lastQuery(), and I have now marked this question as Solved. Thanks all.

              1 Reply Last reply
              1

              1/6

              15 Sept 2017, 15:52

              • Login

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