我如何在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)
}

有没有一些解决方法来启用它? 就像一个想法:

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);
}

还有其他一些方法可能可以完成此操作,例如sqlite3_get_auxdata函数,但我并不真正了解api的工作原理。


context不是你的查询的一部分,所以它不会在触发器函数中可用。

毕竟,您发送用于更新表的SQL语句可能(应该)根本不知道触发器!

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

上一篇: How can I pass variables meant for a trigger function in SQLite?

下一篇: Assembly Function calling x64 VC++