Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery Insert multiple rows (MySQL)

QSqlQuery Insert multiple rows (MySQL)

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    juansta_-
    wrote on last edited by
    #1

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

    INSERT INTO table(c1,c2,...)
    VALUES 
       (v11,v12,...),
       (v21,v22,...),
        ...
       (vnn,vn2,...);
    

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved