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()
发布了57 篇原创文章 · 获赞 73 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_40450867/article/details/102721188