I wrote this stuff a long time ago for a C++ application and made a post on my blog (link at bottom). Here's a quick summary (since the article is quite long and contains file attachments):
in SQLite you write callbacks in raw c++
header file (file.h)
#include <iostream>
#include <sqlite3.h>
#include <cstdlib>
#include <assert.h>
void memberfunction( const unsigned char* text1, const unsigned char* text2, const unsigned char* text3);
void mcallback(sqlite3_context *context, int argc, sqlite3_value **argv);
It defines the two additional functions we’ll need for the next step (actual our C++ side function and a SQLite stored procedure)
implementation file (file.cpp)
This function will be called from our trigger from the DB.
void memeberfunction( const unsigned char* text1, const unsigned char* text2, const unsigned char* text3)
{
cout << “inserted: ” << text1 << ” ” << text2 << ” ” << text3 <<”n”;
}
And now a SQLite store procedure to call our function. A SQLite trigger will invoke this.
void mcallback(sqlite3_context *context, int argc, sqlite3_value **argv)
{
assert( argc == 3);
//SQLite types must be converted to C++ types
const unsigned char* text1 = sqlite3_value_text(argv[0]);
const unsigned char* text2 = sqlite3_value_text(argv[1]);
const unsigned char* text3 = sqlite3_value_text(argv[2]);
memeberfunction( text1, text2, text3);
}
We don't have to limit the function to just a simple print. This function could have moved data to another table.
register the stored procedure to the DB
error = sqlite3_create_function(conn, “mcallback”, 3, SQLITE_UTF8, NULL, &mcallback, NULL, NULL);
create the SQLite Trigger
We need the stored procedure since triggers are built on top of them.
error = sqlite3_exec(conn, “create temp trigger callbacktrigger after insert on hello for each row begin select mcallback(new.first_column, new.second_column, new.exclamation_pt); end;”, 0, 0, 0);
The trigger is only temporary. This way it won’t stick around in the database after execution terminates (of our whole program). Our callback wont be around after then anyways and the trigger could (and will given it’s nature) cause major problems if we use the database outside this application.
The ORIGINAL POST