Ruby中采用PG访问Postgresql表及function
2012-04-11 17:53
288 查看
原文:Rails access the pg db
在rails和ruby中,在我查找要访问postgresql时,总是发现所需要的库为: gem install postgres-pr,gem install postgres,也就是使用postgres这个库来完成与pg交的数据交换,当我采用ruby 直接使用 require 'postgres' 时发现最新的postgres要降版本才可以去ruby调用,具体的原因没找到,因为降版本时根本没办法那个postgres库的版本安装起来,所以就查rails new test -d=postgresql生成的源代码,才发现rails中用的是pg这个库,故而直接用pg这个库实现pg访问和存储过程。
require 'pg'
class Testpg
# To change this template use File | Settings | File Templates.
#dbh = PGConn.connect("",5432,"postgres","400hao.cn","mytest_development")
conn = PG.connect( :dbname => 'mytest_development', :host => 'localhost', :port => 5432,:user => 'postgres' , :password => 'mypassword' )
res = conn.query("SELECT getallblogs(\'myref\');fetch all from myref;")
#res = conn.query("SELECT * FROM \"public\".blogs;")
#res = conn.query("")
#循环列出所有行和列
res.values.collect do |row|
puts row.collect {|col| "%-15s" % [col] }.join( '' )
end
#遍历每条记录的name值
res.each do |row|
puts row["name"]
end
res.clear
conn.close
end
在rails和ruby中,在我查找要访问postgresql时,总是发现所需要的库为: gem install postgres-pr,gem install postgres,也就是使用postgres这个库来完成与pg交的数据交换,当我采用ruby 直接使用 require 'postgres' 时发现最新的postgres要降版本才可以去ruby调用,具体的原因没找到,因为降版本时根本没办法那个postgres库的版本安装起来,所以就查rails new test -d=postgresql生成的源代码,才发现rails中用的是pg这个库,故而直接用pg这个库实现pg访问和存储过程。
require 'pg'
class Testpg
# To change this template use File | Settings | File Templates.
#dbh = PGConn.connect("",5432,"postgres","400hao.cn","mytest_development")
conn = PG.connect( :dbname => 'mytest_development', :host => 'localhost', :port => 5432,:user => 'postgres' , :password => 'mypassword' )
res = conn.query("SELECT getallblogs(\'myref\');fetch all from myref;")
#res = conn.query("SELECT * FROM \"public\".blogs;")
#res = conn.query("")
#循环列出所有行和列
res.values.collect do |row|
puts row.collect {|col| "%-15s" % [col] }.join( '' )
end
#遍历每条记录的name值
res.each do |row|
puts row["name"]
end
res.clear
conn.close
end
相关文章推荐
- php采用curl访问域名返回405 method not allowed提示的解决方法
- 采用HttpURLConnection访问服务端
- ubuntu webrick服务器或者rubymine使用80端口访问或者运行ruby项目
- spring security 采用角色控制访问权限
- 采用URL访问资源,隐藏真实地址
- asp.net中访问SQL Server数据库采用windows集成安全验证
- gridview 采用存储过程分页,提高访问效率。
- 外网访问PG数据库,如何赋予IP访问权限
- SpringCloud(第 045 篇)链接Mysql数据库简单的集成Mybatis、ehcache框架采用MapperXml访问数据库
- 采用UTF-8解决Ruby on Rails程序的中问题
- 主引导区修复工具MbrFix:Function failed. Error 5: 拒绝访问。
- 采用Cookie记录上次访问记录
- 远程访问Function时报错Remote table-valued function calls are not allowed.
- 访问Index function 两次
- vue项目使用手机浏览器访问时报错:Uncaught TypeError: Object.assign is not a function
- 采用ip限制访问次数
- ruby访问mysql
- spring security 采用角色控制访问权限
- springmc采用.jpg为后缀名访问请求问题分析
- ruby中访问控制和字符串,对象引用