Original article: rails access the PG DB
In rails and Ruby, when I find that I want to access PostgreSQL, I always find that the required libraries are: Gem install Postgres-PR, Gem install Postgres, that is to say, the Postgres library is used to exchange data with PG. When I use Ruby to directly use require 'postgres', I find that the latest Postgres version must be downgraded before Ruby can be called, the specific cause is not found, because the version of the Postgres library cannot be installed when the version is downgraded, so check the version generated by rails new test-D = PostgreSQL.Source codeIn rails, the PostgreSQL database is used. Therefore, the PostgreSQL database is directly used to implement PG access and Storage Processes.
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 => 'your s ',: password => 'mypassword ')
Res = conn. Query ("select getallblogs (\ 'myref \ '); fetch all from myref ;")
# Res = conn. Query ("select * From \" public \ ". blogs ;")
# Res = conn. Query ("")
# Listing all rows and columns cyclically
Res. Values. Collect do | row |
Puts row. Collect {| Col | "%-15 s" % [col]}. Join ('')
End
# Traverse the name value of each record
Res. Each do | row |
Puts row ["name"]
End
Res. Clear
Conn. Close
End