Friday, June 5, 2009

Mysql connectivity in ruby while database is allocated at LINUX machine

db = Mysql.real_connect(ip, Username, password, database_name)

username is that user which has all the privileges from mysql(CRUD).

now we should handle some exception handling to this code of line.

begin
# connect to the MySQL server
db = Mysql.real_connect(ip, Username, password, database_name)
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
end


oops! i forgot to require mysql file. obviously, i have to know the address where i am going to kill the real culprit.

so gusy require "mysql"

so lets go to attain the purpose of doing that mysql connectivity.

insertion of data into a specific table:

db.query("insert into table_name values(x,yz1,'For testing',null)")

edit a perticular record of a perticular table:

db.query("update table_name set column_1 = NULL , column_2 = NULL where Column_3 = 1")

deletion of a record of a perticular table:

db.query("DELETE from table_name where id > 2")

No comments:

Post a Comment