#!/usr/bin/ruby -w
require "rubygems"
require "mysql"
begin
     # connect to the MySQL server
     dbh = DBI.connect("DBI:Mysql:TESTDB:localhost", 
                        "nyros", "root")
     # 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

I want to connect and access MySQL database through ruby programming. But i am getting this error when executing the ruby code in my terminal.

我想通过ruby编程连接和访问MySQL数据库。但是我在终端中执行ruby代码时遇到此错误。

Error:

/.rvm/rubies/ruby-1.9.3 p547/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 'require': cannot load such file -- mysql (LoadError)
from /home/nyros/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 'require'
from task.rb:3:in '<main>'

1 个解决方案

#1


I suggest using mysql2. It is a mysql library for Ruby.

我建议使用mysql2。它是Ruby的mysql库。

For example:

require 'mysql2'

client = Mysql2::Client.new(:host => HOST, :username => USERNAME, :database => DATABASE)
client.query("select * from tabel_name;")

If you don't want to use mysql2, you also can refer to this link.

如果您不想使用mysql2,您也可以参考此链接。

更多相关文章

  1. 关于mysql无法添加中文数据的问题以及解决方案
  2. 寻找解决方案,以每100速率1显示div随机而不创建其他div
  3. 从终端(iOS)运行React Native App出错
  4. 等待执行所有ajax回调的最佳解决方案
  5. 【问题解决方案】ImportError: No module named 'pygal'
  6. Python 黏包及黏包解决方案
  7. 无法安装ndg-httpsclient或者我的解决方案错误
  8. linux 的终端字体色和背景色的修改方法(三)
  9. 终端至上!将Vim打造成全新的集成开发环境

随机推荐

  1. 我可以在运行PHP的64位系统上将PHP_INT_S
  2. 如何在PHP中自动设置用户的语言环境?
  3. PHP常用的文件操作函数集锦
  4. 一些Apache、Php和Mysql的优化配置选项
  5. php的最快排序,放弃老师教的消耗大量资源
  6. Nginx 502报错(django+nginx,而非php-fmp
  7. 什么是客户端准备语句?
  8. 在PHP和MySQL中同时插入来自多个表单的多
  9. 关于php的命名空间
  10. 在解释语言上使用非常大的整数时,会产生意