QSqlQuery Insert multiple rows (MySQL)

  • MySql allows for insertion of multiple rows using a single INSERT INTO statement,

    INSERT INTO table(c1,c2,...)

    Is there any way to duplicate this using QSqlQuery? At the moment I can only...

    • Add rows one-at-a-time
    • Prepare my own long INSERT INTO statement (Using QString)

    I was hoping this could be handled by adding a std::vector (or other valid container type) during the bindValue method call... Is this possible?

  • Lifetime Qt Champion

    See https://doc.qt.io/qt-5/qsqlquery.html#execBatch - don't know if the driver supports it.
    But using a transaction + a simple loop will do the same. It will be a little bit slower I would guess but as long as you don't insert thousands of rows it's not measurable.

Log in to reply