Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert Multiple Rows using MySQL INSERT VALUES syntax
    primarykey
    data
    text
    <p>I am looking for a ruby-specific solution to insert multiple rows using the <a href="https://dev.mysql.com/doc/refman/5.1/en/insert.html" rel="nofollow noreferrer">INSERT..VALUES syntax</a></p> <p>Currently I am generating individual insert statements, but this is very inefficient from a database perspective. I found some related questions, but not exactly what I'm looking for.</p> <p><strong>Example INSERT Statements (currently):</strong></p> <pre><code>INSERT INTO qux SELECT 1,3,'foo'; INSERT INTO qux SELECT 4,11,'bar'; INSERT INTO qux SELECT 12,19,'baz'; </code></pre> <p><a href="https://stackoverflow.com/questions/10022178/insert-multiple-records-using-ruby-on-rails-active-record">INSERT multiple records using ruby on rails active record</a></p> <p>The above question uses ActiveRecord to achieve what I am looking to do, but I am looking for a pure ruby approach.</p> <p><a href="https://stackoverflow.com/questions/9288593/how-do-i-perform-transactions-with-ruby-mysql2">how do I perform transactions with ruby mysql2</a></p> <p>The above question uses transactions to achieve a result similar to my needs. However this is still not as efficient as using one INSERT VALUES statement.</p> <p><strong>Example data:</strong></p> <pre><code>start,end,name 1,3,foo 4,11,bar 12,19,baz </code></pre> <p><strong>INSERT VALUES Statement (desired):</strong></p> <pre><code>INSERT INTO qux VALUES (1,3,'foo'),(4,11,'bar'),(12,19,'baz'); </code></pre> <p>Any gem/codebase that already exists with this functionality?</p> <p>UPDATE: I do not use and do not plan to use ActiveRecord in this particular project. I interact directly with the MySQL database and would love to know of any solution to my problem. One option may be building the functionality myself; however I don't want to re-invent the wheel here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload