从 sqlite3 触发器启动 Python 脚本 [英] Launch a Python Script from a sqlite3 Trigger

查看:66
本文介绍了从 sqlite3 触发器启动 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 sqlite3 有一个触发器来启动 python 脚本?我有一个表,其中包含我正在监控网页上的评论的项目列表.有些项目没有.我有一个脚本,它定期检查项目并根据它为每个项目找到的内容更新项目表.我想拥有它,以便在更新项目表时,触发器可以启动一个 python 脚本,然后该脚本可以根据从其他站点提取的数据运行和更新其他表.

Is it possible to have sqlite3 have a trigger that will launch a python script? I have a table that has a list of items for which I am monitoring reviews on a web page. Some items don't have any. I have a script that checks the items on a regular basis and updates the table of items based on what it finds for each item. I would like to have it so that when the table of items is updated a trigger can kick off a python script that can then run and update other tables based on data it pulls from other sites.

这甚至可能吗?

推荐答案

SQL 语句(也在触发器内部)可以调用用户定义的函数,这些函数是用 create_function:

SQL statements (also inside triggers) can call user-defined functions, which are created with create_function:

import sqlite3

def hello(x):
    print "Hello"

con = sqlite3.connect(":memory:")
con.create_function("hello", 1, hello)
cur = con.cursor()
cur.execute("CREATE TABLE t(x)")
cur.execute("CREATE TRIGGER tt AFTER INSERT ON t BEGIN SELECT hello(NEW.x); END;")
cur.execute("INSERT INTO t VALUES(1)")

该函数允许更改数据库;文档 说:

The function is allowed to change the database; the documentation says:

允许应用程序定义的函数调用其他 SQLite 接口.但是,此类调用不得关闭数据库连接,也不得完成或重置运行该函数的准备好的语句.

An application-defined function is permitted to call other SQLite interfaces. However, such calls must not close the database connection nor finalize or reset the prepared statement in which the function is running.

这篇关于从 sqlite3 触发器启动 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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