Psycopg2 - 像值一样返回字典
在本文中,我们将讨论如何使用 psycopg2 返回类似字典的值。
我们可以使用 psycopg2 PostgreSQL 驱动程序返回类似字典的值,无论是否使用 extras 模块,本文将讨论这两者。 psycopg.extras 为 Psycopg2 提供了各种好东西。它保存辅助函数和类,直到找到更好的分布位置。
不使用 psycopg。附加模块:
首先导入所需的包并使用 psycopg2.connect() 方法与 PostgreSQL 数据库建立连接。和一个使用 cursor() 类的游标,然后我们执行 select SQL 语句从表中检索行。 cursor.fetchall() 方法将输出作为元组提供,我们使用切片来获取值并以字典的形式打印出来。
示例:使用 psycopg2 返回类似字典的值
Python3
import psycopg2
conn = psycopg2.connect(
database="codes", user='postgres', password='pass',
host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''CREATE TABLE continent_codes(code varchar(3), name char(20),
concatenated_column varchar(30));'''
cursor.execute(sql)
sql2 = '''COPY continent_codes(code,name,
concatenated_column)
FROM '/private/tmp/continent_codes.csv'
DELIMITER ','
CSV HEADER;'''
cursor.execute(sql2)
sql3 = '''select * from continent_codes;'''
cursor.execute(sql3)
results = cursor.fetchall()
for row in results:
print("code: {}".format(row[0]))
print("name: {}".format(row[1]))
print("concatenated_column: {}".format(row[2]))
conn.commit()
conn.close()
Python3
import psycopg2.extras
conn = psycopg2.connect(
database="codes", user='postgres', password='pass',
host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
sql = '''CREATE TABLE continent_codes(code varchar(3), name char(20),
concatenated_column varchar(30));'''
cursor.execute(sql)
sql2 = '''COPY continent_codes(code,name,
concatenated_column)
FROM '/private/tmp/continent_codes.csv'
DELIMITER ','
CSV HEADER;'''
cursor.execute(sql2)
sql3 = '''select * from continent_codes;'''
cursor.execute(sql3)
results = cursor.fetchall()
for row in results:
print("code: {}".format(row['code']))
print("name: {}".format(row['name']))
print("concatenated_column: {}".format(row['concatenated_column']))
conn.commit()
conn.close()
输出 :
code: AF
name: Africa
concatenated_column: AFAfrica
code: NA
name: North America
concatenated_column: NANorth America
code: OC
name: Oceania
concatenated_column: OCOceania
code: AN
name: Antartica
concatenated_column: ANAntartica
code: AS
name: Asia
concatenated_column: ASAsia
code: EU
name: Europe
concatenated_column: EUEurope
code: SA
name: South America
concatenated_column: SASouth America
使用 psycopg.extras 模块
RealDictCursor 是一个游标,它使用真正的 dict 作为行的基本类型。此游标不允许正常索引来获取数据。只能借助表的列名键来获取数据。
句法:
cursor = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
游标子类作为 cursor_factory 参数传递给 connect() 以便连接的 cursor() 方法将生成此类的对象。 cursor_factory 是 conn.cursor() 类中的一个参数。
句法:
psycopg2.extras.RealDictCursor(*args, **kwargs)
此游标使用 realdict 作为行的基本类型。
示例:使用 psycopg2 返回类似字典的值
Python3
import psycopg2.extras
conn = psycopg2.connect(
database="codes", user='postgres', password='pass',
host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
sql = '''CREATE TABLE continent_codes(code varchar(3), name char(20),
concatenated_column varchar(30));'''
cursor.execute(sql)
sql2 = '''COPY continent_codes(code,name,
concatenated_column)
FROM '/private/tmp/continent_codes.csv'
DELIMITER ','
CSV HEADER;'''
cursor.execute(sql2)
sql3 = '''select * from continent_codes;'''
cursor.execute(sql3)
results = cursor.fetchall()
for row in results:
print("code: {}".format(row['code']))
print("name: {}".format(row['name']))
print("concatenated_column: {}".format(row['concatenated_column']))
conn.commit()
conn.close()
输出:
code: AF
name: Africa
concatenated_column: AFAfrica
code: NA
name: North America
concatenated_column: NANorth America
code: OC
name: Oceania
concatenated_column: OCOceania
code: AN
name: Antartica
concatenated_column: ANAntartica
code: AS
name: Asia
concatenated_column: ASAsia
code: EU
name: Europe
concatenated_column: EUEurope
code: SA
name: South America
concatenated_column: SASouth America