postgre 数据库 python操作

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)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。