且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 SQLite3 中使用 Qt QSqlDriver::subscribeToNotification?

更新时间:2023-02-01 08:34:34

Qt sqlite 驱动程序中的 subscribeToNotification 实现依赖于 sqlite3_update_hook sqlite C API 的函数.然而,Qt 驱动程序不转发执行的操作,只是转发表名,这应该是传递给 subscribeToNotification的神秘的 anEventId 参数>.简而言之,您可以监听任何表中发生的事件(假设它是一个 rowid 表,但是它通常是)将表名传递给 subscribeToNotification 方法.但是,当您的插槽捕获 notification 信号时,您只知道该表中发生了一个操作,但(遗憾的是)Qt 不会告诉您是哪一个(INSERT、UPDATE 或 DELETE).

The subscribeToNotification implementation in the Qt sqlite driver relies upon the sqlite3_update_hook function of the sqlite C API. The Qt driver, however, is not forwarding the operation performed, just the table name, and that should be the misterious anEventId argument to pass to subscribeToNotification. In short, you can listen to events occurring in any table (given it is a rowid table, but it generally is) passing the table name to the subscribeToNotificationmethod. When your slot catches the notification signal, though, you only know that an operation occurred in that table, but (sadly) Qt won't tell you which one (INSERT, UPDATE, or DELETE).

所以,给定一个QSqlDriver * driver:

driver->subscribeToNotification("mytable1");
driver->subscribeToNotification("mytable2");
driver->subscribeToNotification("mytable3");

然后在您的插槽中:

void MyClass::notificationSlot(const QString &name)
{
    if(name == "mytable1")
    {
        // do something
    }
    else if(name == "mytable2")
    {

    //etc...