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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on 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

      JonBJ 1 Reply Last reply
      3
      • artwawA artwaw

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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 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

          jsulmJ 1 Reply Last reply
          0
          • M mostefa

            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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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

            JonBJ 1 Reply Last reply
            2
            • jsulmJ jsulm

              @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".

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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

              • Login

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