psycopg3 中的客户端编码缺失,但出现错误

问题描述 投票:0回答:1

所以我在 psycopg 2 中看到,有一个

set_client_encoding('UTF8')
功能。 在 psycopg 3 文档中,它写道:

client_encoding 消失了

我在这里创建连接:

conn = psycopg.connect(conninfo, row_factory = dict_row, autocommit = autocommit, client_encoding="UTF8")

但是当我实际使用连接时:

cur.execute(query, params)

我收到编码错误。

UnicodeEncodeError: 'charmap' codec can't encode character '\u202f' in position 15: character maps to <undefined> encoding with

“cp1252”编解码器失败

我如何在 pyscopyg 3 中解决这个问题?

postgresql psycopg3
1个回答
0
投票

您应该检查 psycopg 检测到的编码,例如:

import psycopg as pg

conn = pg.connect('dbname=test user=my_user password=my_password')
print(conn.info.encoding)

您可以在

conninfo
(dns)中定义参数:

conn = pg.connect('dbname=test user=my_user password=my_password client_encoding=utf8')
print(conn.info.encoding)

或使用 postgres

SET
命令:

conn = pg.connect('dbname=test user=my_user password=my_password')
with conn.cursor() as cursor:
    cursor.execute('set client_encoding to utf8')
print(conn.info.encoding)
© www.soinside.com 2019 - 2024. All rights reserved.