So part of the purpose of using (and reusing) prepared statements is that the database driver performs less work. Coming from Perl, I am used to preparing a SQL query and storing a reference to that prepared query so that I can later bind some values and execute the query, many times if necessary.
With Qt C++ (Qt 5.1) I try to do this:
class MyClass {
[...]
QSqlDatabase db;
QSqlQuery insert_query;
};
MyClass::MyClass() {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("whatever");
db.open();
insert_query = QSqlQuery(db);
insert_query.prepare("insert into players (firstname, lastname) values(:firstname, :lastname)"));
}
void MyClass::MyMeth(QString firstname, QString lastname) {
insert_query.bindValue(":firstname", firstname); //COMPILE ERROR
}
error: no matching member function for call to 'bindValue'
note: candidate function not viable: 'this' argument has type 'const QSqlQuery', but method is not marked const
But I want to bind new values in the prepared query outside of the MyClass constructor. I found this, but I am suspicious that it is cargo-cult because calling QSqlQuery::prepare("query") more than once is effectively a noop if "query" remains the same (even if the QSqlQuery object is different). Is this true with some drivers? Otherwise, what am I missing? How am I supposed to reuse prepared queries?