因此,当前,当我执行SELECT查询并检索数据时,我必须获得如下结果:
connection = psycopg2.connect(user="admin",password="admin",host="127.0.0.1",port="5432",database="postgres_db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM user")
users = cursor.fetchall()
for row in users:
print(row[0])
print(row[1])
print(row[2])
我想做的是,使用列名而不是整数,像这样:
for row in users:
print(row["id"])
print(row["first_name"])
print(row["last_name"])
有可能吗?如果可以,怎么办?