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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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')");
No, you need to prepare the query and those are structurally different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.