我试图在Ruby中创建一个简单的脚本来访问MySQL数据库并根据用户输入运行查询。
如果用户提供错误的登录信息或尝试SELECT
FROM
不存在的表或列,我希望用户重新输入 信息这导致问题,而不必重新输入所有信息。
我希望此代码根据查询的哪个部分引发错误引发异常:
#!/usr/local/bin/ruby
require "mysql2"
require "watir"
require "csv"
Mysql2::Client.default_query_options.merge!(:as => :array)
mysql = Mysql2::Client.new(:host => "1.2.3.4", :username => "usr", :password => "pass123", :database => "db")
db = "db"
puts "Please enter all `table`.`column` pairs you wish to select from; separated by a comma and space."
tCP = gets.chomp.split(", ")
dynQ = "SELECT "
tCP.each {|pair| dynQ << (db + "." + pair + ", ") }
puts "Please enter the `table` you wish to select from:"
tF = db + "." + gets.chomp
dynQ = dynQ.chop!.chop! + " FROM " + tF + " LIMIT 10;"
report = mysql.query(dynQ)
begin
report = mysql.query(dynQ)
report.each {|row| puts row}
puts "Query Successful!"
rescue Mysql2::Error => e
puts e.errno
puts e.error
puts "Please re-nter all `table`.`column` pairs separated by, ', '."
tCP = gets.chomp!.split(", ")
dynQ = "SELECT "
tCP.each {|pair| dynQ << (db + "." + pair + ", ") }
tF = db + "."
puts "Now re-enter the `table` to select `FROM`:"
tF << gets.chomp!
dynQ = dynQ.chop!.chop! + " FROM " + tF + " LIMIT 10;"
retry
end
例如,如果我SELECT
来自不存在的表或列,我会得到:
/Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:107:in '_query': Table 'db.table_dne' doesn't exist (Mysql2::Error)
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:107:in 'block in query'
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:106:in 'handle_interrupt'
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:106:in 'query'
from file.rb:23:in '<main>'
然而,如果我从一个不存在的表中选择那个选择,我会得到同样的东西:
/Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:107:in '_query': Unknown column 'db.table_dne' doesn't exist (Mysql2::Error)
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:107:in 'block in query'
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:106:in 'handle_interrupt'
from /Users/guy/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.2/lib/mysql2/client.rb:106:in 'query'
from file.rb:23:in '<main>'
理想情况下,我希望rescue
基于控制台中出现的异常并让用户重新输入tCP
或tF
,而不是仅仅获得更一般的例外。
我没有运气看了MySQL2 documentation。
答案 0 :(得分:1)
让我们打破你的尝试:
rescue Mysql2::Error => e
puts e.errno
puts e.error
rescue
让您的脚本保持正常运行Mysql2::Error => e
取自Error
类并存储在e
puts e.errno
puts
错误编号到控制台puts e.error
puts
错误说明到控制台然后,以下begin
循环应该适合您:
begin
report = mysql.query(dynQ)
report.each {|row| puts row}
puts "Query Successful!"
rescue Mysql2::Error => e
if e.errno === 1146
puts e.error
tF = db + "."
puts "Now re-enter the `table` to select `FROM`:"
tF << gets.chomp!
elsif e.errno === 1054
puts e.error
puts "Please re-nter all `table`.`column` pairs separated by, ', '."
tCP = gets.chomp!.split(", ")
dynQ = "SELECT "
tCP.each {|pair| dynQ << (db + "." + pair + ", ") }
else
# puts e.errno
puts e.error
puts "Please re-nter all `table`.`column` pairs separated by, ', '."
tCP = gets.chomp!.split(", ")
dynQ = "SELECT "
tCP.each {|pair| dynQ << (db + "." + pair + ", ") }
tF = db + "."
puts "Now re-enter the `table` to select `FROM`:"
tF << gets.chomp!
end
dynQ = dynQ.chop!.chop! + " FROM " + tF + " LIMIT 10;"
retry
end