如何区分异常

How to distinguish between exceptions

我正在尝试在 Ruby 中创建一个简单的脚本来访问 MySQL 数据库并根据用户输入运行查询。

如果用户提供错误的登录信息或尝试 SELECT FROM table 或不存在的列,我希望用户重新输入 导致问题的信息,而不必重新输入所有信息。

我希望此代码根据查询的哪一部分抛出错误来引发异常:

#!/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 来自 table 或不存在的列,我会得到:

/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>'

然而,如果我从不存在的 table 中进行选择,我会得到完全相同的结果:

/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 基于控制台中出现的异常并让用户重新输入 tCPtF 而不是仅仅给出更一般的异常。

我看了 MySQL2 documentation 运气不好。

让我们分解你的尝试:

rescue Mysql2::Error => e
  puts e.errno
  puts e.error
  1. rescue 保留您的脚本 运行
  2. Mysql2::Error => e 取自 Error class 并存储在 e
  3. puts e.errno puts 控制台的错误号
  4. 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