您的位置:首页 > 编程语言 > Python开发

postgre 数据库 python操作

2015-01-28 12:13 127 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: