Cannot retrieve the SQL query string from QSqlQuery?



  • 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....



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



  • @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!



  • 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


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.