python - How to extend `psycopg2.extras.DictCursor`? -
i want use python3-psycopg2 (2.5.4) cursor can:
1) access query result in dict-like manner
2) log every sql executed cursor logging
module automatically
i tried following code, didn't work, did wrong? can extend psycopg2.extensions.cursor
, not allow me 1).
class loggingcursor(psycopg2.extras.dictcursor): def __init__(self, *args, **kwargs): # didn't work or super().__init__(*args, **kwargs) # without these 2 lines def execute(self, sql, args=none): beautiful import app # logger = blah blah logger.debug(self.mogrify(sql, args).decode()) psycopg2.extensions.cursor.execute(self, sql, args) cursor = db_conn.cursor(cursor_factory=loggingcursor)
when cursor.execute(some_sql)
, gives me:
file "some_file.py", line 123, in some_function some_var = cursor.fetchone() file "/usr/local/lib/python3.4/dist-packages/psycopg2/extras.py", line 63, in fetchone res = super(dictcursorbase, self).fetchone() file "/usr/local/lib/python3.4/dist-packages/psycopg2/extras.py", line 139, in __init__ self._index = cursor.index attributeerror: 'loggingcursor' object has no attribute 'index'
how changing
psycopg2.extensions.cursor.execute(self, sql, args)
to
super().execute(sql, args)
so base class execute()
method called?
Comments
Post a Comment