How can I pass variables meant for a trigger function in SQLite?

/* 
// this is the trigger definition
CREATE TEMPORARY TRIGGER 'insertTrigger' INSTEAD OF INSERT ON 'foo' 
BEGIN 
    SELECT bar(NEW.id, NEW.timestamp); "
END; 
//*/

void insert(sqlite3 *db, char *id, char *timestamp, void *context){
    exec_query(db, "INSERT INTO foo(id,timestamp) VALUES(?,?)", id, timestamp);
}

void bar(sqlite3_context *context, int argc, sqlite3_value **argv){
    char *id = (char*)sqlite3_value_text(argv[0]);
    char *timestamp = (char*)sqlite3_value_text(argv[1]);
    //how do I get context* ???
    //this must be a thread safe function (i.e. multiple threads have their own sqlite3* and all execute queries (possibly including this one)
}

Is there some workaround to enable this? Just as an idea:

void insert(sqlite3 *db, char *id, char *timestamp, void *context){
    sqlite3_mutex_enter(sqlite3_db_mutex(db));
    sqlite3_user_setdata(db, context); //this doesnt exist
    exec_query(db, "INSERT INTO foo(id,timestamp) VALUES(?,?)", id, timestamp);
    sqlite3_mutex_leave(sqlite3_db_mutex(db));
}

void bar(sqlite3_context *context, int argc, sqlite3_value **argv){
    char *id = (char*)sqlite3_value_text(argv[0]);
    char *timestamp = (char*)sqlite3_value_text(argv[1]);
    void *context_ = sqlite3_user_data(context);
}

There are a few other methods it seems that might be able to accomplish this such as the sqlite3_get_auxdata functions, but I dont really understand how that api works.


context is not part of your query so it will not be available in the trigger function.

After all, the SQL statement you send to update your table might (should) not know about the triggers at all!

链接地址: http://www.djcxy.com/p/84338.html

上一篇: C将参数传递为void

下一篇: 我如何在SQLite中传递用于触发器函数的变量?