insert multiple rows


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def self.insert_multiple_rows(columns, values)
  2. values_statement = ""
  3. values.each { |row| values_statement << "(#{columns.length.times}
  4.  
  5. connection.execute(<<-EOF)
  6. INSERT INTO #{table_name} (#{columns.join(",")})
  7. VALUES
  8. #{values_statement}
  9. EOF
  10. end
  11.  
  12. Now you'll be able to call, for example:
  13.  
  14. columns = ["username","real_name", "password"]
  15. values = [
  16. ["divoxx", "Rodrigo", "foo"],
  17. ["johng", "John Gary", "yay"]
  18. ]
  19. User.insert_multiple_rows(columns, values)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.