QSqlQuery::bindValue problem
hi
since the update to qt 4.5 i can't get QSqlQuery::bindValue to work
i tried google and searched the forum but couldn't find an answer to this question.
the following sample code works perfectly using qt 4.3.4, but with qt 4.5 i get this error:
QSqlError(-1, "QPSQL: Unable to create query", "ERROR: syntax error at or near "(" at character 10")
Code:
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QVariant>
int main(int argc, char **argv) {
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("testuser");
db.setPassword("testpw");
db.open();
q.prepare("SELECT :bla FROM myTable");
q.bindValue(":bla","foo");
if(q.exec()) {
while(q.next()) {
qDebug() << "output:" << q.value(0).toString();
}
}
else {
qDebug() << q.lastError();
}
}
if i do a
Code:
q.prepare("SELECT foo FROM myTable");
everything works just fine :/ but i can't get bindValue to work in qt 4.5 :(
does anyone know what's the problem here?
Re: QSqlQuery::bindValue problem
Im not sure if this ought to work, since I always use value() method to get the selected columns.
If it shoud work at all, it might have to be bound as "out" or inout variable, the default is however QSql::in
In case your intention is to dynamically change column names of the query, maybe this is a solution:
query.prepare(QString("select %1 from mytable").arg("foo"));
Re: QSqlQuery::bindValue problem
Quote:
Originally Posted by
segfault
if i do a
Code:
q.prepare("SELECT foo FROM myTable");
everything works just fine :/ but i can't get bindValue to work in qt 4.5 :(
does anyone know what's the problem here?
But I think that after bindValue Your query looks like :
Code:
SELECT 'foo' FROM myTable
Try look at PostgreSQL logs.
Re: QSqlQuery::bindValue problem
That seems to be right.
Maybe the OP will make clear what he wants to achive.
Re: QSqlQuery::bindValue problem
Quote:
Originally Posted by
Lesiok
But I think that after bindValue Your query looks like :
Code:
SELECT 'foo' FROM myTable
Try look at PostgreSQL logs.
Code:
SELECT 'foo' FROM myTable
or
SELECT foo FROM myTable
doesn't matter, since they work both.
my problem is that i used
Code:
q.prepare("SELECT :bla FROM test");
q.bindValue(":bla",someVariable);
in a project using qt < 4.5 to create some select queries and it doesn't work in qt 4.5 anymore, it works fine for insert statements though.
but since a solution like
Code:
query.
prepare(QString("select %1 from mytable").
arg("foo"));
works fine, i will use this. (should have done it like that in the first place)
thanks everybody for your help.
Re: QSqlQuery::bindValue problem
Quote:
Originally Posted by
segfault
Code:
SELECT 'foo' FROM myTable
or
SELECT foo FROM myTable
doesn't matter, since they work both.
Yes they both work, but they do different things of course.
The first will always return the text foo, no matter what is actually in the table, and the second will return the content of the column with name foo.
Re: QSqlQuery::bindValue problem
Quote:
Originally Posted by
seneca
Yes they both work, but they do different things of course.
The first will always return the text foo, no matter what is actually in the table, and the second will return the content of the column with name foo.
i know :) sorry, i just wanted to point out that i would be happy with either of them, instead of the error i got :)