简体   繁体   中英

Call Python script from SQL trigger

I have the following scenario: users can log in and update certain tables, and whenever that table is updated I would like to call a Python script that will query the same database for additional information (joins) and then use Django's email system to email all users in that group (same role) that information.

My current methodology is to create a user-defined function in SQLite3, and then create a trigger that will call that function (which is linked to a Python file). Is this possible with SQLite?

Django app, SQLite and Python.

import sqlite3

def email_materials_submittal(project_id):
    print(project_id)


con = sqlite3.connect('../db.sqlite3')
con.create_function("FUNC_EMS", 1, email_materials_submittal)
con.commit()
cur = con.cursor()
cur.execute("CREATE TRIGGER TRIG_EMS AFTER INSERT ON core_level BEGIN SELECT FUNC_EMS(NEW.project_id); END;")
con.commit()
con.close()

I want SQL to call an external Python script whenever a new row is inserted into a certain table. Tried the code above, but SQLite throws the error: no such function: FUNC_EMS

This task is much easier using signals for some notifications and a combination of custom management commands and crontab jobs for others (date reminders). Django is awesome!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM