- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在编写一个 Qt 应用程序,其中不同的模型可以插入/删除/更新同一个表。当一个模型更改数据库时,我希望其他模型能够收到更改通知,以便他们可以相应地更新 View 。
在 SQLite 中监控插入、删除和更新的最佳方法似乎是使用 QSqlDriver::subscribeToNotification
然后对通知信号使用react。我知道语法是这样的:
db.driver()->subscribeToNotification("anEventId");
但是,我不确定 anEventId
是什么意思。 anEventId
是 SQLite 提供的常量,还是我使用触发器或其他方式将这些特定事件编码到 SQLite 中,然后订阅它们?
最佳答案
Qt sqlite 驱动程序中的 subscribeToNotification
实现依赖于 sqlite3_update_hook sqlite C API 的功能。然而,Qt 驱动程序并没有转发执行的操作,只是转发表名,这应该是传递给 subscribeToNotification
的神秘的 anEventId 参数>。简而言之,您可以监听任何表中发生的事件(假设它是一个 rowid table ,但它通常是)将表名传递给 subscribeToNotification
方法。但是,当您的槽捕捉到 notification
信号时,您只知道该表中发生了一个操作,但(遗憾的是)Qt 不会告诉您是哪一个(INSERT、UPDATE 或 DELETE)。
因此,给定一个 QSqlDriver * 驱动程序
:
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...
关于c++ - 如何在 SQLite3 中使用 Qt QSqlDriver::subscribeToNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204329/
我正在用 Qt 编写程序,但遇到了一个小问题。这编译得很好,但我有这个: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available
我在使用 QSqlDriver 和 QSqlDatabase 时遇到了一个奇怪的问题。 在我的程序中,我打开了一个数据库连接。当主窗口关闭并且所有对象都被销毁时,持有 QSqlDatabase 的对象
我正在编写一个 Qt 应用程序,其中不同的模型可以插入/删除/更新同一个表。当一个模型更改数据库时,我希望其他模型能够收到更改通知,以便他们可以相应地更新 View 。 在 SQLite 中监控插入、
我是一名优秀的程序员,十分优秀!