import psycopg2 as pg
# create database connectiontry: conn = pg.connect("dbname='template1' user='dbuser' host='localhost' password='dbpass'")except: print "Unable to connect to database"
# create database cursorcur = conn.cursor()
# execute SQL and fetch resultscur.execute("""SELECT datname from pg_database""")rows = cur.fetchall()
print "\nShow database results:\n"for row in rows: print row[0]
Wednesday, January 5, 2011
Connecting to PostgreSQL with Python and Psycopg2
Basic syntax for making a database connection, executing and retrieving data:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment