Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy向每个查询添加注释_Python_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy向每个查询添加注释

Python SQLAlchemy向每个查询添加注释,python,sqlalchemy,Python,Sqlalchemy,我试图让SQLAlchemy在我提交的每个查询中添加一条注释 从文档中我知道我可以做这样的事情: from sqlalchemy.sql.expression import Insert @compiles(Insert) def prefix_inserts(insert, compiler, **kw): return compiler.visit_insert(insert.prefix_with("/* comment */"), **kw) 但是我不知道如何才能对每一个查询

我试图让SQLAlchemy在我提交的每个查询中添加一条注释

从文档中我知道我可以做这样的事情:

from sqlalchemy.sql.expression import Insert

@compiles(Insert)
def prefix_inserts(insert, compiler, **kw):
    return compiler.visit_insert(insert.prefix_with("/* comment */"), **kw)

但是我不知道如何才能对每一个查询都这样做,不管是什么类型的查询,而不必在
编译器中列出每个查询。

我会使用事件。有一个例子:

from sqlalchemy.engine import Engine
from sqlalchemy import event

@event.listens_for(Engine, "before_cursor_execute", retval=True)
def comment_sql_calls(conn, cursor, statement, parameters,
                                    context, executemany):
    statement = statement + " -- some comment"
    return statement, parameters