Ruby DBI:连接到MariaDB数据库时出错

gzszwxb4  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(91)

我想按照本指南(https://www.tutorialspoint.com/ruby/ruby_database_access.htm)的第一步操作,当然有一个问题。
这就是我的代码看起来的样子:

#!/usr/bin/ruby -w

require "dbi"

begin
   # connect to the MySQL server
   dbh = DBI.connect("DBI:MariaDB:TESTDB:localhost", "testuser", "test123")
   # get server version string and display it
   row = dbh.select_one("SELECT VERSION()")
   puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
   puts "An error occurred"
   puts "Error code:    #{e.err}"
   puts "Error message: #{e.errstr}"
ensure
   # disconnect from server
   dbh.disconnect if dbh
end

字符串
这就是我收到的

/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:74: warning: assigned but unused variable - meth
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:263: warning: assigned but unused variable - e2
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/date.rb:42: warning: previous definition of initialize was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/time.rb:16: warning: previous definition of initialize was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/timestamp.rb:21: warning: previous definition of initialize was here
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:6: warning: assigned but unused variable - e
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_nullable?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_nullable? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old can_be_null?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of can_be_null? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_indexed?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_indexed? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_primary?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_primary? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_unique
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_unique was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of size was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of size= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of length was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of length= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of decimal_digits was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of decimal_digits= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of default_value was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of default_value= was here
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:110: warning: assigned but unused variable - civil
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:111: warning: assigned but unused variable - time
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles.rb:12: warning: optional boolean argument is obsoleted
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/database.rb:218: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/database.rb:224: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:307: warning: assigned but unused variable - cols
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:324: warning: assigned but unused variable - cols
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:358: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:364: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
Traceback (most recent call last):
        5: from dziad.rb:7:in `<main>'
        4: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect'
        3: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver'
        2: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver'
        1: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `synchronize'
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:300:in `block in load_driver': Unable to load driver 'MariaDB' (underlying error: uninitialized constant DBI::DBD::MariaDB) (DBI::InterfaceError)


我一直在使用GPT聊天来安装MariaDB并验证gem版本。然而,尽管在上面花费了大量的时间,我还是无法使下面的代码工作。
我的设置:WSL版本- 5.15.90.1-microsoft-standard-WSL 2 Ruby版本- ruby 2.7.4p191(2021-07-07 revision a21 a3 b7 d23)[x86_64-linux-gnu] Debian版本- 11.7 dbi(0.4.5,0.4.0)mysql 2(0.5.5)My MariaDB在命令sudo service mariadb status后运行良好。

j91ykkif

j91ykkif1#

1.宝石dbi在15年前就被废弃了。我不确定它是否适用于Ruby 2.x。
1.使用gem sequel连接数据库。使用gem mysql2作为Sequel的驱动程序连接MariaDB数据库。

require 'bundler/inline'

gemfile(true) do
  source "https://rubygems.org"
  gem 'sequel'
  gem 'mysql2'
end

#                             username password         database
#                                ^        ^                 ^
DB = Sequel.connect('mysql2://testuser:test123@localhost/testdb')
row = DB.select(Sequel.lit('version()')).to_a
puts "Server version: #{row[0]}"

字符串

相关问题