I'm struggling with implementing a notification system using triggers and custom functions. Here's what I'd like to accomplish: SQLite notifies me for every time a row is inserted, updated, or deleted in a given table. Here's how I've attempted it so far (so far for insertions only):

I added a custom function named "notifyObservers" via sqlite3_create_function.

CREATE TABLE foo( x, y, z );

CREATE TRIGGER foo_insert AFTER INSERT ON foo FOR EACH ROW
   WHEN NEW.x > 0
   BEGIN
      SELECT notifyObservers( "insert", x ) FROM foo;
   END;

INSERT INTO foo VALUES( 1, 2, 3 );
   -- notifyObservers( "insert", 1 ) gets called -- as expected

INSERT INTO foo VALUES( 4, 5, 6 );
   --notifyObservers( "insert", 1 ) gets called again -- not desired
   --notifyObservers( "insert", 4 ) gets called -- OK

INSERT INTO foo VALUES( 7, 8, 9 );
   --notifyObservers( "insert", 1 ) gets called yet again -- not desired
   --notifyObservers( "insert", 4 ) gets called again -- not desired
   --notifyObservers( "insert", 7 ) gets called -- OK


Can anyone suggest how I might accomplish what I'm seeking?

-Eric



Reply via email to