栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python,Bash或CLI中的SQLite数据更改通知回调

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python,Bash或CLI中的SQLite数据更改通知回调

可以从SQLite CLI使用这些回调…

仔细阅读SQLite源代码,看起来该功能似乎未在CLI源代码中的任何地方使用,因此我怀疑您可以通过CLI来完成此工作。

…或者来自Bash …

不知道那是什么意思。

…或来自Python?

它不是通过标准

sqlite3
模块公开的,但是可以与
ctypes
模块一起使用。

如果是这样,怎么办?

这是一个通过

ctypes
…使用它的快速示例。

from ctypes import *# Define some symbolsSQLITE_DELETE =  9SQLITE_INSERT = 18SQLITE_UPDATE = 23# Define our callback function## 'user_data' will be the third param passed to sqlite3_update_hook# 'operation' will be one of: SQLITE_DELETE, SQLITE_INSERT, or SQLITE_UPDATE# 'db name' will be the name of the affected database# 'table_name' will be the name of the affected table# 'row_id' will be the ID of the affected rowdef callback(user_data, operation, db_name, table_name, row_id):    if operation == SQLITE_DELETE:        optext = 'Deleted row'    elif operation == SQLITE_INSERT:        optext = 'Inserted row'    elif operation == SQLITE_UPDATE:        optext = 'Updated row'    else:        optext = 'Unknown operation on row'    s = '%s %ld of table "%s" in database "%s"' % (optext, row_id, table_name, db_name)    print(s)# Translate into a ctypes callbackc_callback = CFUNCTYPE(c_void_p, c_void_p, c_int, c_char_p, c_char_p, c_int64)(callback)# Load sqlite3dll = CDLL('libsqlite3.so')# Holds a pointer to the database connectiondb = c_void_p()# Open a connection to 'test.db'dll.sqlite3_open('test.db', byref(db))# Register callbackdll.sqlite3_update_hook(db, c_callback, None)# Create a variable to hold error messageserr = c_char_p()# Now execute some SQLdll.sqlite3_exec(db, b'create table foo (id int, name varchar(255))', None, None, byref(err))if err:    print(err.value)dll.sqlite3_exec(db, b'insert into foo values (1, "Bob")', None, None, byref(err))if err:    print(err.value)

…打印出来…

Inserted row 1 of table "foo" in database "main"

在第一次运行中

table foo already existsInserted row 2 of table "foo" in database "main"

在第二轮



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号