PDA

View Full Version : QSqlQuery::bindValue problem



segfault
10th March 2009, 15:13
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")



#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QVariant>


int main(int argc, char **argv) {
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("testuser");
db.setPassword("testpw");
db.open();

QSqlQuery q(db);
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


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?

seneca
10th March 2009, 15:20
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"));

Lesiok
10th March 2009, 15:45
if i do a


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 :

SELECT 'foo' FROM myTable
Try look at PostgreSQL logs.

seneca
10th March 2009, 15:57
That seems to be right.

Maybe the OP will make clear what he wants to achive.

segfault
10th March 2009, 16:48
But I think that after bindValue Your query looks like :

SELECT 'foo' FROM myTable
Try look at PostgreSQL logs.




SELECT 'foo' FROM myTable
or
SELECT foo FROM myTable

doesn't matter, since they work both.


my problem is that i used


QSqlQuery q(db);
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


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.

seneca
10th March 2009, 17:59
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.

segfault
11th March 2009, 07:27
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 :)