Python读写postgresql

需要安装psycopg2

增删改查可通过cursor.execute实现

import psycopg2
connection = psycopg2.connect(database="yourdatabase",
             user="postgres", password="postgres",
             host="127.0.0.1",port="5432")
cursor = connection.cursor()
#执行SQL语句
cursor.execute("""SELECT * FEOM yourtable;""")
#获取数据
data=cursor.fetchall()
print(data)
#提交事务
connection.commit()
#关闭连接
cursor.close()
connection.close()

 

你可能感兴趣的:(Python)