Posted on 2015-02-22 12:27
笑看人生 阅读(446)
评论(0) 编辑 收藏
程序代码如下:
1 require 'postgres'
2
3 conn = PGconn.connect("localhost", 5432, '', '', "ruby", "username", "password")
4
5 begin
6 res = conn.exec('SELECT * FROM test')
7 res.each do |row|
8 row.each do |column|
9 print column
10 end
11 end
12 rescue Postgres::PGError => e
13 puts "Error select."
14 puts "Error code: #{e.err}"
15 puts "Error message: #{e.errstr}"
16 ensure
17 conn.close() if conn
18 end
数据库名:ruby,用户名:username,密码:password
在运行程序之前,需要预先install postgreSQL的驱动,安装方法如下,在命令行输入以下命令:
1 gem install postgres-pr
参考网址
http://troubleshooters.com/codecorn/ruby/database/index.htm
通过gem安装dbi,一直提示错误。
gem install dbi
gem install dbd-pg