Skip to content

Instantly share code, notes, and snippets.

@dolzenko
Created February 3, 2014 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolzenko/8784547 to your computer and use it in GitHub Desktop.
Save dolzenko/8784547 to your computer and use it in GitHub Desktop.
['mysql2', 'sequel', 'pp'].each(&method(:require))
DB = Sequel.connect('mysql2://root@localhost/tmp')
DB.tables.each {|t| DB.drop_table(t) }
def t(name, columns, *rows)
DB.create_table(name) do
columns.each.with_index do |column, ind|
type = rows[0][ind].class.to_s
send type, column
end
end
rows.each do |row|
DB[name].insert(Hash[columns.zip(row)])
end
end
def q(query)
pp DB[query].to_a
end
# Create table & populate
t :users,
[:id, :name],
[42, 'Bob']
# Print query result
q 'SELECT * FROM users'
# When ready to share paste to http://sqlfiddle.com/
# mysqldump --compact tmp | grep -v '^/' | grep -v 'LOCK'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment