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

Dewsworld
  • 13,367
  • 23
  • 68
  • 104

1 Answers1

14

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

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Davita
  • 8,928
  • 14
  • 67
  • 119
  • 1
    Is there a way to execute different queries in a batch? Like: `query.exec("insert into person values(104, 'Roberto', 'Robitaille')");` `query.exec("insert into house values('Foo Street', 'Bar')");` – ephtron Dec 17 '18 at 00:17