Today, i found a nice way to connect to mysql database. Not so much for now but at least it works well. There is a method to print out a message in color green to the prompt.(Dont really think its that necessay, just for beautification). The require method is used to load the module. The escape sequence ( “33[30m”) is what doing the coloration of the text. There is also an exception handler.
Referrence: http://www.kitebird.com/articles/ruby-mysql.html by Paul DuBois
require 'mysql'
#a function call to color the text
def look
print "33[32m Connection Successful"
#h = sleep 1
puts "33[30m"
end
begin
#connect to the Mysql server
dbh = Mysql.real_connect('localhost','ben','rubeehax0r','ben1')
look
#get server version string and display it
puts "Server Version : #{dbh.get_server_info} "
rescue Mysql::Error => e
puts "33[31m Error Code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
print "33[30m"
ensure
#disconnect from the server
dbh.close if dbh
end

No comments yet
Comments feed for this article