python操作postgresql数据库

时间:2023-12-26 09:11:19
import psycopg2
conn = psycopg2.connect(database="baoxin", user="odoo", password="odoo", host="127.0.0.1", port="")
cur = conn.cursor()
cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num integer,data varchar);")
cur.execute("SELECT * FROM product_category;")
rows = cur.fetchall() # all rows in table
print(rows)
for i in rows:
print(i)
conn.commit()
cur.close()
conn.close()