mysql - Ruby and mysql2: Looping Until Connection is Error Free -
context: writing simple dynamic query in ruby using mysql2
gem.
attempt:
#!/usr/local/bin/ruby require "mysql2" puts "please enter title of report:" title = gets.chomp mysql2::client.default_query_options.merge!(:as => :array) puts "please enter host, username, password , database in order:" hst = gets.chomp user = gets.chomp pass = gets.chomp db = gets.chomp begin mysql = mysql2::client.new(:host => hst, :username => user, :password => pass, :database => db) rescue mysql2::error => e puts e.errno puts e.error retry puts "error: please try again." puts "enter host, username, password , database:" hst = gets.chomp! user = gets.chomp! pass = gets.chomp! db = gets.chomp! end puts "successfully accessed #{db}!"
note that:
rescue mysql2::error => e puts e.errno puts e.error
works mysql
gem, but:
rescue mysql2::standarderror => e puts e.errno puts e.error
does not work mysql2
gem.
finally, error in terminal:
imac:workspace guy$ ruby file.rb please enter title of report: title please enter host, username, password , database in order: 1.2.3.4 username15 password123 db_one file.rb:19:in `rescue in <main>': uninitialized constant mysql2::standarderror (nameerror) did mean? standarderror file.rb:17:in `<main>'
edit after answer: leaving :host
:hst
gave me following error:
2002 can't connect local mysql server through socket '/tmp/mysql.sock' (2)
mysql2
gem has defined mysql2::error
, not `mysql2::standarderror.
you need rescue mysql2::error
refer mysql2 github source more info.
Comments
Post a Comment