Created
August 1, 2014 05:33
-
-
Save kminiatures/5a9855f70d0341c9ad46 to your computer and use it in GitHub Desktop.
mysql select を配列でかえす
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
| 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