ruby访问mysql
2016-01-17 16:56
281 查看
$su -
~# apt-get install ruby-dev
# gem install mysql2 --
--with-mysql-config=/opt/mysql/bin/mysql_config
Building native extensions. This could take
a while...
Successfully installed mysql2-0.3.17
1 gem installed
Installing ri documentation for mysql2-0.3.17...
Installing RDoc documentation for mysql2-0.3.17...
:~# gem install mysql --
--with-mysql-config=/opt/mysql/bin/mysql_config
Fetching: mysql-2.9.1.gem (100%)
Building native extensions. This could take
a while...
Successfully installed mysql-2.9.1
1 gem installed
Installing ri documentation for mysql-2.9.1...
Installing RDoc documentation for mysql-2.9.1...
mysql测试代码
require 'mysql'
begin
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME,
'utf8')
db = Mysql.real_connect("127.0.0.1", "用户名",
"密码", "数据库名")
db.query("SET NAMES utf8")
rslt = db.query("select * from 表名")
while row = rslt.fetch_row do
puts row[0]
puts row[1]
end
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if
e.respond_to?("sqlstate")
ensure
db.close if db
end
mysql2测试代码
require 'mysql2'
client = Mysql2::Client.new(:host => "127.0.0.1", :username
=> "用户名",:password=>"密码",:database=>"数据库名")
results = client.query("select * from 表名");
results.each do |hash|
puts hash.map { |k,v| "#{k} = #{v}"
}.join(", ")
end
(pg)访问postgreSQL
gem install pg --
--with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config
require 'pg'
conn = PG.connect( :dbname => '数据库名', :host => '主机名',
:port => 5432,:user => '用户名' , :password => '密码' )
results = conn.exec( "SELECT * from 表名" )
results.each do |hash|
puts hash.map { |k,v| "#{k} = #{v}"
}.join(", ")
end
~# apt-get install ruby-dev
# gem install mysql2 --
--with-mysql-config=/opt/mysql/bin/mysql_config
Building native extensions. This could take
a while...
Successfully installed mysql2-0.3.17
1 gem installed
Installing ri documentation for mysql2-0.3.17...
Installing RDoc documentation for mysql2-0.3.17...
:~# gem install mysql --
--with-mysql-config=/opt/mysql/bin/mysql_config
Fetching: mysql-2.9.1.gem (100%)
Building native extensions. This could take
a while...
Successfully installed mysql-2.9.1
1 gem installed
Installing ri documentation for mysql-2.9.1...
Installing RDoc documentation for mysql-2.9.1...
mysql测试代码
require 'mysql'
begin
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME,
'utf8')
db = Mysql.real_connect("127.0.0.1", "用户名",
"密码", "数据库名")
db.query("SET NAMES utf8")
rslt = db.query("select * from 表名")
while row = rslt.fetch_row do
puts row[0]
puts row[1]
end
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if
e.respond_to?("sqlstate")
ensure
db.close if db
end
mysql2测试代码
require 'mysql2'
client = Mysql2::Client.new(:host => "127.0.0.1", :username
=> "用户名",:password=>"密码",:database=>"数据库名")
results = client.query("select * from 表名");
results.each do |hash|
puts hash.map { |k,v| "#{k} = #{v}"
}.join(", ")
end
(pg)访问postgreSQL
gem install pg --
--with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config
require 'pg'
conn = PG.connect( :dbname => '数据库名', :host => '主机名',
:port => 5432,:user => '用户名' , :password => '密码' )
results = conn.exec( "SELECT * from 表名" )
results.each do |hash|
puts hash.map { |k,v| "#{k} = #{v}"
}.join(", ")
end
相关文章推荐
- 180行ruby代码搞定游戏2048
- ruby函数回调的实现方法
- ruby函数回调的实现方法
- CocoaPods安装使用
- ruby each do 用法
- 如何在Mac OS X上安装 Ruby运行环境
- dock 下安装ruby搭建rails 服务
- Ruby笔记
- Ruby安装
- ruby语言是什么东西
- RVM切换ruby版本
- 《Java & Ruby 代码分析系列》之基本的类实例对比
- ruby postgres 连接
- CocoaPods的下载和安装及常见问题
- ruby on rails mac 基本配置
- 如何在Mac OS X上安装 Ruby运行环境
- 新人千万不要在 Windows 上使用 Ruby on Rails
- Ruby对象、变量和常量
- Ruby的基础学习
- ruby、gem、rails之间的关系