python:连接postgre数据库
记录一下创新班写的东西
import psycopg2
conn = psycopg2.connect(database="id_info", user="postgres", password="123", host="127.0.0.1", port="5432")
print("Opened database successfully")
# 数据库插入语句
# cur = conn.cursor()
#
# cur.execute("INSERT INTO ID_INFO (ID,NAME,SEX,NATION,BIRTH,ADDRESS,ID_NUMBER) \
# VALUES (1, 'UZI', '男','汉', '20201225', '广州大学','4413811')")
#
# conn.commit()
# conn.close()
# 数据库查询操作
cur = conn.cursor()
cur.execute("SELECT id, name, sex,nation,birth,address,id_number from ID_INFO")
rows = cur.fetchall()
for row in rows:
print(row[0])
print(row[1])
print(row[2])
print(row[3])
print(row[4])
print(row[5])
print(row[6])
conn.close()