Launch a Python Script from a sqlite3 Trigger Launch a Python Script from a sqlite3 Trigger sqlite sqlite

Launch a Python Script from a sqlite3 Trigger


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

import sqlite3def 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:

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.