Using PostgreSQL to access PostgreSQL tables and functions in ruby

Source: Internet
Author: User
Tags postgres version

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.