QSqlQuery bindValue() Function fails to Bind
-
Hi
I am having a trouble with QSqlQuery class' bindValue()
The code:query.prepare("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=:table"); query.bindValue(":table", table); if(query.exec() == false) { qDebug() << "DataIO::createTableIf:1: " << query.lastError().text(); } qDebug() << query.executedQuery();
The qDebug() prints this
"SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=?"
I can't understand why bindValue() doesn't binds the value and just binds "?".
Is this something with runtime? I may as well use the old way:
query.exec("SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' AND NAME=" + table);
But i want to use the right way of doing things. Thanks in advance for any help.
-
@Corpse0327 Did you check if you get the output ?
As per the doc it will return result same aslastQuery()
but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that caseexecutedQuery()
will show the values. More info here. -
@p3c0 said:
@Corpse0327 Did you check if you get the output ?
As per the doc it will return result same aslastQuery()
but in case if the db does not support placeholders then a new query is formed with bound values and I guess in that caseexecutedQuery()
will show the values. More info here.Well, the function returns the latest succesfully executed query. It returns the query i added only with placeholders replaced with quos. So it means it executed query with quos.
I checked it from sqldb using SqliteMan. By the way it is not just limited to this query. At any query i prepared placeholders are replaced by quos.
-
@Corpse0327 Since sqlite db supports placeholders new query with those bound values will not be formed and it will show question mark as I understand from the doc.
-
@p3c0 Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.
I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.
-
Forgive me if i misunderstood. You are saying that it would show question marks but would work properly right? Since Sqlite supports placeholders.
Yes.
I tested it many times and, no. It just executes the query with question marks. I have changed my code to 'without bindValue()' version. Everything works fine.
Are you sure ? I hope you are checking in the following manner after query execution
while(query.next()) qDebug() << query.value(0).toString(); //get value at 0 column
Also does
query.exec()
returns false in this case ? -
@p3c0 Thank you for your help
I have realized that
- If query execution is a failure, than lastQuery() returns a string with either placeHolders or placeHolders replaced with quos. Making the user thinking something with binding is wrong.
- If query is succesful, lastQuery() returns either placeHolders replaced with quos or actual values.
I am going to further test things out and see if something may be improved with bindValue() and make a request based on that.