How do I notify a process of an SQLite database change done in a different process?

前端 未结 7 2514
南笙
南笙 2021-02-19 16:27

Let\'s say I have two or more processes dealing with an SQLite database - a \"player\" process and many \"editor\" processes.

The \"player\" process reads the database

7条回答
  •  眼角桃花
    2021-02-19 17:16

    SQLite has an update_hook function which does what you want.

    SQLite C Interface

    Data Change Notification Callbacks

    void *sqlite3_update_hook(
        sqlite3*,
        void(*)(void *,int ,char const *,char const *,sqlite3_int64),
        void*
    );
    

    The sqlite3_update_hook() interface registers a callback function with the database connection identified by the first argument to be invoked whenever a row is updated, inserted or deleted in a rowid table. Any callback set by a previous call to this function for the same database connection is overridden.

    Unfortunately it isn't exposed by the Python sqlite module...

    Here is a slightly hacky workaround that digs into the C api (from Python code) to make use of it: https://stackoverflow.com/a/16920926

提交回复
热议问题