Skip to content

Instantly share code, notes, and snippets.

@lazzarello
Created January 31, 2011 21:08
Show Gist options
  • Save lazzarello/804820 to your computer and use it in GitHub Desktop.
Save lazzarello/804820 to your computer and use it in GitHub Desktop.
a generic insert statement builder that takes a table name, an array of keys and an array of values as arguments
def build_insert_statement(table,keys,values)
raise "keys must be an Array" unless keys.kind_of? Array
raise "values must be an Array" unless values.kind_of? Array
raise "table name must be a String" unless table.kind_of? String
statement = "INSERT INTO #{table}("
keys.each do |k|
statement << "#{k},"
end
statement.chop! << ") VALUES("
# ))<>(( huh huh
values.each do |v|
statement << "#{v},"
end
statement.chop! << ")"
return statement
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment