QtSql multiple query execution

Dewsworld picture Dewsworld · Mar 17, 2012 · Viewed 9.7k times · Source
 ...
 query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
 query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
 ...

Can these be bind in a single query.exec() ?

Answer

Davita picture Davita · Mar 17, 2012

I guess you are trying to execute in a batch your query. Yes, qt supports this scenario.

bool QSqlQuery::execBatch ( BatchExecutionMode mode = ValuesAsRows )

Executes a previously prepared SQL query in a batch. All the bound parameters have to be lists of variants. If the database doesn't support batch executions, the driver will simulate it using conventional exec() calls. Returns true if the query is executed successfully; otherwise returns false.

 QSqlQuery q;
 q.prepare("insert into myTable values (?, ?)");

 QVariantList ints;
 ints << 1 << 2 << 3 << 4;
 q.addBindValue(ints);

 QVariantList names;
 names << "Harald" << "Boris" << "Trond" << QVariant(QVariant::String);
 q.addBindValue(names);

 if (!q.execBatch())
     qDebug() << q.lastError();

http://doc.qt.io/archives/qt-4.7/qsqlquery.html#execBatch