I wrote this stuff a long time ago for a C++ application and made a post on my blog. Here's a quick summary (since the original 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 (actually 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 stored 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 (for example) 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 won't be around after then anyway and the trigger could (and will, given it's nature) cause major problems if we use the database outside this application.