d_maniger06 wrote:
ok..i have this sample code:

[code]
void printIfInsert()
{
    printf( "Insertion was done in t1\n" );
}

void createTrigger( sqlite** db, sqlite_vm** pvm )
{
    char* errMsg;
    const char** psql = NULL;
    const char* sql = "CREATE TRIGGER onInsertTrig AFTER INSERT on t2 BEGIN
printIfInsert; END";

    if( sqlite_create_function( *db, "printIfInsert", 0, &printIfInsert,
NULL ) != 0 )
    {
        printf( "hahahaha! no function was created\n" );
        exit(1);
    }

    if ( sqlite_compile(*db, sql, psql, &*pvm, &errMsg) != SQLITE_OK )
    {
        printf( "CREATE TRIGGER FAILED: %s\n", errMsg );
    }
}
[/code]

im not really sure if this is correct..i have read a documentation of
sqlite_create_function in sqlite.org but its not really that clear to
me..can you please guide me on this?..what i wanted to happen is that every
time, i insert on table t2, there should be something printed on the
console..actually, this is just for test..what i really wanted is it to log
in a log file every time a new record is inserted in the table..but my
concern is just i dont know how to call that function (printIfInsert()) in
the trigger statement..

please help me on this..

thank you and God bless!.. c",)


John Stanton-3 wrote:
I that case you need to implenment a custom function and launch it from a trigger.

d_maniger06 wrote:
im sorry but i havent get your point..im rather new in sqlite and i just
need
to make a c program, that when i insert on a table in my database,
something
is written on a file, or even just be printed out in the console saying
that
my table in my database has been updated/inserted..


John Stanton-3 wrote:

d_maniger06 wrote:

good day!..

i would just like to ask if you can set a trigger to call on a
non-database
event (e.g. writing to a file) whenever an update/insert has been made
to
the database..im using c programming language for this..if this is
possible,
can u give me some links or direct examples on how to do this?..

thank you and God bless!..
If you are intercepting some event in your C program why not just make the event execute some SQL?

-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------



-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------





Here is a code fragment of a custom function calling some external capability to extend Sqlite and the function which registers it with Sqlite. You then call the function in your trigger like any regular SQL function. In your case you would probably use a mutex to synchronize access to your file, and append your log record to it.

#if HAS_JAVASCRIPT
/*-- exec_js ------------------------------------------------------
Execute a Javascript function. The global property "result" is returned to this function.
The first argument must be the Javascript function.  There may be a
variable number of subsequent arguments.*/
static void exec_js(sqlite3_context *context, int argc, sqlite3_value **argv) {

 int                  argk;
 char               *argw[16];
 char                bf[256];
 DBCONNP   dbp;
 char                *jsp;
 int                  lg;
 void              *p;

 if (argc < 1)  return;   /*Must have at least one arg.*/

jsp = (char *)sqlite3_value_text(argv[0]); /*Get the Javascript source.*/

 /*DB connection.*/
 p = sqlite3_user_data(context);
 dbp = (DBCONNP)p;

 /*Create an array of arguments.*/
 argk = 0;
 argc--;
 while ((argk < argc) && (argk < 16)) {
   argw[argk] = (char *)sqlite3_value_text(argv[argk + 1]);
   argk++;
 }  /*while*/

 /*Create a Javascript session*/
 lg = js_session(jsp, dbp, bf, 256, argk, argw);

 if (lg > 0) sqlite3_result_text(context, (char*)bf, lg, SQLITE_TRANSIENT);
}  /*exec_js*/
#endif

/*-- load_axs4_fns -----------------------------------------------
Load AXS4 user functions into SQLITE.  Meant to be called just after
a database connection is opened.  By convention a pointer to the DB
control block is loaded into the userdata pointer to make it accessable
to the function.*/
int load_axs4_fns (DBCONNP dbb) {

 char  *funcnm;
 int     rc;

#if HAS_JAVASCRIPT
 funcnm = "exec_js";
 rc = sqlite3_create_function(dbb->db, funcnm, -1, SQLITE_ANY,
                              (void *)dbb, exec_js, NULL, NULL);
 if (rc != SQLITE_OK) {
   errormet("User function load failed for ", funcnm);
   return(1);
 }  /*if*/
#endif
 return(0);
}  /*load_axs4_fns*/

-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to