Posted By

rduv on 10/20/06


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


insert multiple rows


 / Published in: Ruby
 

  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  

You need to login to post a comment.