gpt4 book ai didi

mysql - Ruby 和 mysql2 : Looping Until Connection is Error Free

转载 作者:行者123 更新时间:2023-11-29 11:41:20 25 4
gpt4 key购买 nike

上下文:我正在使用 mysql2 gem 在 Ruby 中编写一个简单的动态查询。

尝试:

#!/usr/local/bin/ruby
require "mysql2"

puts "Please enter the title of this Report:"
title = gets.chomp

Mysql2::Client.default_query_options.merge!(:as => :array)

puts "Please enter the host, username, password and 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 the host, username, password and database:"
hst = gets.chomp!
user = gets.chomp!
pass = gets.chomp!
db = gets.chomp!
end

puts "Successfully accessed #{db}!"

请注意:

rescue Mysql2::Error => e
puts e.errno
puts e.error

可以与 mysql gem 一起使用,但是:

rescue Mysql2::StandardError => e
puts e.errno
puts e.error

mysql2 gem 一起使用。

最后,终端中的错误:

iMac:workspace guy$ ruby File.rb
Please enter the title of this Report:
title
Please enter the host, username, password and database in order:
1.2.3.4
username15
password123
db_one
File.rb:19:in `rescue in <main>': uninitialized constant Mysql2::StandardError (NameError)
Did you mean? StandardError
from File.rb:17:in `<main>'

回答后编辑:将 :host 保留为 :hst 给了我以下错误:

2002
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

最佳答案

mysql2 gem 定义了 Mysql2::Error,而不是`Mysql2::StandardError。

你需要拯救Mysql2::Error

引用mysql2 Github source了解更多信息。

关于mysql - Ruby 和 mysql2 : Looping Until Connection is Error Free,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35732660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com