PDA

View Full Version : QtSql: execBatch() with SELECT statements



herrmarder
28th January 2010, 13:04
I am not an expert SQL user and I do not have much experience with the QtSql Module, I hope I am not missing something obvious:

I want to retrieve a list of blobs from my (sqlite) database with a batch command. But it does not work properly. isSelect() is not true for the query and I cannot get the results of the query.
Am I using the command correctly? Or should I simply do consecutive exec() commands. Will that have an effect on the speed?

Here is my sample code:


QString query_string = QString(
"SELECT my_blob_column FROM my_table WHERE id = ? ;"
);
q.prepare(query_string);
q.addBindValue(ids_in_qvariantlist);

if (!q.execBatch()) {...}

qDebug() << q.isSelect(); // is false

while (q.next()) { // is never true
qDebug() << q.value(0).toByteArray()
}


edit:
I am running Qt 4.5.3 on OpenSuse 11.2

JohannesMunk
28th January 2010, 13:25
Why don't you build a list of the ids you want to retrieve. (I assume, that its not just all or some simple subset...)


QString idset = "(";
for (int i=0;i < list.length();++i)
{
idset += QString::number(list.at(i));
if (i < list.length()-1)
idset += ",";
}
idset += ")";

QSqlQuery q;
q.exec("SELECT my_blob_column FROM my_table WHERE id in "+idset+";");
while (q.next())
{
}
..

HIH

Johannes

herrmarder
28th January 2010, 13:43
Thank you Johannes,
I have tried using execBatch() so much that I did not see the obvious workaround :-)
Tobias