Created
January 31, 2011 21:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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