gpt4 book ai didi

python - 我如何使用 Psycopg2 的 LoggingConnection?

转载 作者:太空狗 更新时间:2023-10-29 17:42:47 25 4
gpt4 key购买 nike

我想记录 psycopg2 进行的查询,但是 psycopg2 documentation并没有真正指定应该如何使用 LoggingConnection。

import logging
from psycopg2.extras import LoggingConnection

db_settings = {
"user": "abcd",
"password": "efgh",
"host": "postgres.db",
"database": "dev",
}

conn = LoggingConnection(**db_settings)

报错

LoggingConnection(**db_settings) TypeError: function takes at most 2 arguments (5 given)

最佳答案

似乎设置 connection_factory=LoggingConnection 有效

import logging
import psycopg2
from psycopg2.extras import LoggingConnection

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

db_settings = {
"user": "abcd",
"password": "efgh",
"host": "postgres.db",
"database": "dev",
}

conn = psycopg2.connect(connection_factory=LoggingConnection, **db_settings)
conn.initialize(logger)

cur = conn.cursor()
cur.execute("SELECT * FROM table LIMIT 5")

关于python - 我如何使用 Psycopg2 的 LoggingConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com