Python 在Hook中包含数据?
我在Python模块中使用set_update_hook函数来自动将数据库更改发送到函数。基本上,它是这样工作的:Python 在Hook中包含数据?,python,sqlite,Python,Sqlite,我在Python模块中使用set_update_hook函数来自动将数据库更改发送到函数。基本上,它是这样工作的: from pysqlite2 import connect def hook(conn, op, db_name, table_name, rowid): """Handle notification here. Do not modify the connection!""" with connect(":memory:") as conn: conn.set
from pysqlite2 import connect
def hook(conn, op, db_name, table_name, rowid):
"""Handle notification here. Do not modify the connection!"""
with connect(":memory:") as conn:
conn.set_update_hook(hook)
conn.execute("CREATE TABLE a (int id);")
conn.execute("INSERT INTO a VALUES (1);")
我试图确定发送到hook的信息是否包含在数据库中插入、更新或删除的数据
因此,在执行insert之后,我打印了hook参数的值:
conn = ['DataError', 'DatabaseError', 'Error', 'IntegrityError', 'InterfaceError', 'InternalError', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'Warning', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_update_hook', '_update_hook_cb', '_update_hook_last_error', 'close', 'commit', 'create_aggregate', 'create_collation', 'create_function', 'cursor', 'enable_load_extension', 'execute', 'executemany', 'executescript', 'in_transaction', 'interrupt', 'isolation_level', 'iterdump', 'last_update_hook_error', 'load_extension', 'rollback', 'row_factory', 'set_authorizer', 'set_progress_handler', 'set_trace_callback', 'set_update_hook', 'text_factory', 'total_changes']
op = UpdateHookOps.SQLITE_INSERT
db_name = main
table_name = test
rowid = 3
但数据在哪里?在插入或更新的情况下,我是否必须执行单独的查询来检索它
谢谢:该机制是对以下各项的包装: 回调的第一个参数是sqlite3_update_hook的第三个参数的副本。第二个回调参数是SQLITE_INSERT、SQLITE_DELETE或SQLITE_UPDATE,具体取决于导致调用回调的操作。回调的第三个和第四个参数包含指向包含受影响行的数据库和表名的指针。最后一个回调参数是行的rowid。在更新的情况下,这是更新发生后的rowid 因此,您可以获得足够的信息来识别行