Skip to content

Instantly share code, notes, and snippets.

@mkhuda
Created October 12, 2017 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhuda/475cef69bbc9863b832a9f321e447826 to your computer and use it in GitHub Desktop.
Save mkhuda/475cef69bbc9863b832a9f321e447826 to your computer and use it in GitHub Desktop.
MySQL Database Class for Ruby Using Mysql2
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