Created
October 12, 2017 20:07
-
-
Save mkhuda/475cef69bbc9863b832a9f321e447826 to your computer and use it in GitHub Desktop.
MySQL Database Class for Ruby Using Mysql2
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
require "mysql2" | |
class MyDatabase | |
def initialize | |
@client = Mysql2::Client.new( | |
:host => "localhost", | |
:username => "root", | |
:password => "yourpassword", | |
:database => "sinatra" | |
) | |
end | |
def select_all_komedian | |
@hash = Hash.new | |
@array = Array.new | |
@query = @client.query("SELECT * FROM komedian ORDER BY id") | |
@query.each do |a| | |
@hash = {:id => a['id'], :nama => a['nama']} | |
@array << @hash | |
end | |
return @array.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment