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)
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery Insert multiple rows (MySQL)

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 2.0k 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 4 Nov 2019, 02:32 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 4 Nov 2019, 04:50 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

      1/2

      4 Nov 2019, 02:32

      • Login

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