Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QSqlQuery bindValue works with ? but not with :placeholders

I'm working with SQLite, doing insert into table. Folowwing

QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(?)"));
testQuery.bindValue(0, someQStringObg);
testQuery.exec();

works, but

QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(:val)"));
testQuery.bindValue(":val", someQStringObg);
testQuery.exec();

don't. testQuery.lastError().text() returns No query Unable to fetch row

Have no clue why things are that way, but really want to find out.

like image 590
user3136871 Avatar asked Dec 26 '13 13:12

user3136871


1 Answers

Please use prepare as the official example:

QSqlQuery testQuery;
testQuery.prepare("INSERT INTO test(testcol) VALUES(:val)");
testQuery.bindValue(":val", someQStringObj);
testQuery.exec();

The reason for the error is that the query was executed before binding to the corresponding placeholder. You can see the relevant part of the constructor documentation:

If query is not an empty string, it will be executed.

like image 96
lpapp Avatar answered Oct 31 '22 22:10

lpapp