Created
March 28, 2012 20:33
-
-
Save jgraber/2230282 to your computer and use it in GitHub Desktop.
Connect from Ruby to PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pg' | |
require 'ap' | |
def connect(db, user, pw) | |
PGconn.new('localhost', 5432, '', '', db, user, pw) | |
end | |
def show_shops2(conn) | |
conn.exec( "SELECT haendler_id AS id, * from tbl_haendler;" ) do |result| | |
result.each do |row| | |
puts " %7d | %s, %s, %s %s, %s => %s" % row.values_at('id', 'name','strasse', 'plz', 'ort','land', 'web') | |
end | |
result.each do |row| | |
ap row | |
end | |
end | |
end | |
begin | |
conn = connect('****','*','****') | |
#show_movies(conn) | |
show_shops2(conn) | |
rescue PGError=>e | |
puts "Eeeeeeeek!", e | |
ensure | |
conn.close unless conn.nil? | |
puts "Connection closed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment