如何解决SQLite触发器调用用python编写的自定义函数
每当数据库中的某个字段发生更改时,我都希望更新某些文件。因此,我想到了使用触发器,因此不需要循环来一次又一次地检查该字段。不过,数据库更新是由其他程序进行的,因此用户定义的功能必须是永久性的。
我发现了这篇文章Launch a Python Script from a sqlite3 Trigger。它可以工作,但是在这种情况下,函数“ hello”不会保留在数据库中。因此,如果我尝试将新值插入数据库,它将返回Error: no such function: hello
。数据库是否也可能保存该功能,而不仅仅是触发器?还是一旦关闭连接就无法扩展功能?
test.db:
create table mytable (
myid integer primary key,mystring text
);
test.py:
import sqlite3
def hello(id,text):
print("Hey,it worked!")
print(id)
print(text)
con = sqlite3.connect("test.db")
con.create_function("myfunction",2,hello)
cur = con.cursor()
cur.execute("create trigger if exists mytrigger after insert on mytable begin select hello(NEW.myid,NEW.mystring); END;")
# This line activates the trigger but after the script was run,the trigger still exists but the
# function can not be found.
cur.execute("insert into mytable values(1,'test123')")
con.commit()
con.close()
我希望可以使用Sqlite解决此问题。 提前致谢, 塞巴斯蒂安
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。