在Python或bash或CLI SQLite的数据更改通知回调 [英] SQLite Data Change Notification Callbacks in Python or Bash or CLI

查看:155
本文介绍了在Python或bash或CLI SQLite的数据更改通知回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLite的有数据更改通知回调的C API的使用。可这些回调从SQLite的CLI上使用,或击或Python?

如果是这样,怎么样?


解决方案

  

能否这些回调从SQLite的CLI上使用...


通过SQLite的源$ C ​​$ C阅读,它看起来并不像功能在CLI源$ C ​​$ C任何地方使用,所以我怀疑,你可以通过CLI做到这一点。


  

...或...猛砸


不知道你的意思是什么。


  

...或Python?


这不是通过标准的 的sqlite3 模块,但你可以用 ctypes的 模块。


  

如果是这样,怎么样?


下面是通过 ctypes的 ...

使用它的快速N'肮脏的例子

 从ctypes的导入*#定义一些符号
SQLITE_DELETE = 9
SQLITE_INSERT = 18
SQLITE_UPDATE = 23#定义我们的回调函数

#'的user_data'将传递给sqlite3_update_hook第三参数
#'操作'将是一个:SQLITE_DELETE,SQLITE_INSERT或SQLITE_UPDATE
#'数据库名称'将是受影响的数据库的名称
#'TABLE_NAME'将是受影响的表的名称
#'ROW_ID'将是受影响的行的ID
高清回调(USER_DATA,操作,DB_NAME,TABLE_NAME ROW_ID):
    如果操作== SQLITE_DELETE:
        OPTEXT =已删除行
    ELIF操作== SQLITE_INSERT:
        OPTEXT ='插入行
    ELIF操作== SQLITE_UPDATE:
        OPTEXT ='更新的行
    其他:
        OPTEXT ='在行未知操作'
    S =%'%s的表%的数据库%S的%ld个'(OPTEXT,ROW_ID,TABLE_NAME DB_NAME)
    打印■#翻译成一个ctypes回调
c_callback = CFUNCTYPE(c_void_p,c_void_p,c_int的,c_char_p,c_char_p,c_int64)(回调)#sqlite3的负载
DLL = CDLL('libsqlite3.so')#存放了一个指针到数据库的连接
DB = c_void_p()#打开为'test.db的连接
dll.sqlite3_open('test.db的',按地址(DB))#注册回调
dll.sqlite3_update_hook(DB,c_callback,无)#创建一个变量来保存错误信息
ERR = c_char_p()#现在执行一些SQL
dll.sqlite3_exec(DB,创建表foo(ID INT,名称为varchar(255)),无,无,按地址(ERR))
如果错误:
    打印err.value
dll.sqlite3_exec(DB,'插入到foo值(1,鲍勃)',无,无,按地址(ERR))
如果错误:
    打印err.value

...打印出...

 数据库中的表foo的插入第1行的主

...在第一次运行和...

 表foo已经存在
在数据库表中插入foo的第2行主

......在第二次运行。

SQLite has Data Change Notification Callbacks available in the C API. Can these callbacks be used from the SQLite CLI, or from Bash or from Python?

If so, how?

解决方案

Can these callbacks be used from the SQLite CLI...

Reading through the SQLite source code, it doesn't look like that function is used anywhere in the CLI source code, so I doubt you can do it via the CLI.

...or from Bash...

Not sure what you mean by that.

...or from Python?

It's not exposed via the standard sqlite3 module, but you can use it with the ctypes module.

If so, how?

Here's a quick n' dirty example of using it via ctypes...

from ctypes import *

# Define some symbols
SQLITE_DELETE =  9
SQLITE_INSERT = 18
SQLITE_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 row
def 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 callback
c_callback = CFUNCTYPE(c_void_p, c_void_p, c_int, c_char_p, c_char_p, c_int64)(callback)

# Load sqlite3
dll = CDLL('libsqlite3.so')

# Holds a pointer to the database connection
db = c_void_p()

# Open a connection to 'test.db'
dll.sqlite3_open('test.db', byref(db))

# Register callback
dll.sqlite3_update_hook(db, c_callback, None)

# Create a variable to hold error messages
err = c_char_p()

# Now execute some SQL
dll.sqlite3_exec(db, 'create table foo (id int, name varchar(255))', None, None, byref(err))
if err:
    print err.value
dll.sqlite3_exec(db, 'insert into foo values (1, "Bob")', None, None, byref(err))
if err:
    print err.value

...which prints out...

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

...on the first run and...

table foo already exists
Inserted row 2 of table "foo" in database "main"

...on the second run.

这篇关于在Python或bash或CLI SQLite的数据更改通知回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆