/* // 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 语句可能(应该)根本不知道触发器!
关于c++ - 如何在 SQLite 中传递用于触发函数的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21370061/