psycopg3 mogrify:AttributeError:“连接”对象没有属性“mogrify”

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

我正在使用 psycopg3,想看看带有参数的实际 sql 是什么,以便我可以使用 dbweaver 执行它...

         with psycopg.connect(self.connect_str, autocommit=True) as conn:
            if self.log.level == logging.DEBUG:
                  cur = conn.cursor()
                  sql_mogr = cur.mogrify(sql, params)
                  self.log.debug(sql_mogr)
            else:
               self.log.info(f'sql: {sql}, params:{params}')

            df = pd.read_sql(sql, con = conn, params = params)

mogrify 线的结果是:

AttributeError: 'Cursor' object has no attribute 'mogrify'

psycopg3不支持这种方法吗?如果没有,替代解决方案是什么?

psycopg 版本:

psycopg==3.1.18
psycopg-binary==3.1.18
psycopg-pool==3.2.1
python postgresql psycopg2 psycopg3
1个回答
0
投票

mogrify
方法仅适用于ClientCursor类。

>>> conn = psycopg.connect(dbname='test', cursor_factory=psycopg.ClientCursor)
>>> cur = conn.cursor()
>>> q = """select * from users"""

>>> cur.mogrify(q)
'select * from users'
© www.soinside.com 2019 - 2024. All rights reserved.