Created
February 3, 2014 14:14
-
-
Save dolzenko/8784547 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
['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