phoenix13

导航

 

1. python connect postgre

https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL          Using psycopg2 with PostgreSQL

import psycopg2
import pprint


# get a connection, if a connect cannot be made an exception will be raised here conn = psycopg2.connect("dbname='db1' user='postgres' host='localhost' password='123'")# conn.cursor will return a cursor object, you can use this cursor to perform queries cursor = conn.cursor() # execute Query cursor.execute("SELECT * FROM department") # retrieve the records from the database records = cursor.fetchall() #records = cursor.fetchone() # print out the records using pretty print pprint.pprint(records)

 

posted on 2015-01-28 12:13  phoenix13  阅读(278)  评论(0编辑  收藏  举报