Skip to content

Instantly share code, notes, and snippets.

@kminiatures
Created August 1, 2014 05:33
Show Gist options
  • Select an option

  • Save kminiatures/5a9855f70d0341c9ad46 to your computer and use it in GitHub Desktop.

Select an option

Save kminiatures/5a9855f70d0341c9ad46 to your computer and use it in GitHub Desktop.
mysql select を配列でかえす
class MySQL
def initialize(database, password, user = 'root')
@database = database
@password = password
@user = user
end
def select(sql)
rows = []
`mysql -u#{@user} -p#{@password} #{@database} -e "#{sql} \\G"`.strip.split(/\*\*\*\*.*/).each do |result|
next if result == ''
h = {}
result.strip.split("\n").each do |line|
column, value = line.split(":")
h[column.strip.to_sym] = value.strip
end
rows << h
end
rows
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment