当前位置: 首页 > 工具软件 > ruby-pg > 使用案例 >

ruby2.1.0 mysql_ruby安装mysql2,pg模块

颜英博
2023-12-01

$ sudo gem install mysql2 --'--with-mysql-config=/opt/mysql/bin/mysql_config'

$ sudo gem install mysql --'--with-mysql-config=/opt/mysql/bin/mysql_config'

测试

$ cat testmysql2.rb

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模块

sudo gem install pg --'--with-pg-config=/opt/PostgreSQL/9.4/bin/pg_config'

代码:

#!/usr/bin/env ruby

# encoding: utf-8

require 'pg'

# This is a minimal example of a function that can test an existing PG::Connection and

# reset it if necessary.

conn = PG.connect( :dbname => 'mymotif', :host => 'localhost', :port => 5432,:user => 'mymotif' , :password => 'wxwpxh' )

conn.exec( "SELECT * from student" ) do |res|

p res.values

end

如ppa安装后边的config参数可以忽略。

安装oci8

$ cd /opt/ora11g/instantclient_11_2/lib/

$ ln -s ../sdk .

$ sudo gem install ruby-oci8

测试代码:

#!/usr/bin/env ruby

# encoding: utf-8

require 'oci8'

begin

conn = OCI8.new("mymotif", "wxwpxh", "xe")

rslt = conn.exec("select * from STUDENT")

while r=rslt.fetch()

arr=r.join()

print arr+"\n"

end

rslt.close

rescue OCIError

puts '-'*80

puts "Code: " + $!.code.to_s

puts "Desc: " + $!.message

puts '-'*80

ensure

conn.logoff if conn

end

 类似资料: