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 ofQSqlQuery
which returns this, which seems surprising as it's so fundamental? Why noQSqlQuery::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 -
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 birthdaywhile (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
-
@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 beQSqlQuery::lastQuery()
, and I have now marked this question as Solved. Thanks all.