Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QtSql multiple query execution

Tags:

qt

qt4

qtsql

 ...
 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() ?

like image 823
Dewsworld Avatar asked Mar 17 '12 06:03

Dewsworld


1 Answers

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

like image 181
Davita Avatar answered Sep 19 '22 18:09

Davita