gpt4 book ai didi

c++ - 如何在 PostgreSQL 中创建插入、删除或更新命令的信号并在 C++ 中处理它们?

转载 作者:行者123 更新时间:2023-11-29 14:09:41 25 4
gpt4 key购买 nike

当 PostgreSQL 中有插入、删除或更新命令时,我正在尝试找到数据库通知的最佳方式。

我的目标是在数据库发生变化时立即处理它们。

我该怎么做?

最佳答案

我在 Qt 中找到了一个很好的方法。

首先,您需要创建规则来通知您更新已发生:

CREATE RULE table_notification_insert AS ON INSERT TO public.table DO NOTIFY table_inserted;
CREATE RULE table_notification_update AS ON UPDATE TO public.table DO NOTIFY table_updated;
CREATE RULE table_notification_delete AS ON DELETE TO public.table DO NOTIFY table_deleted;

然后,您可以使用 Qt 接收每个通知(“table_inserted”、“table_updated”、“table_deleted”),如下所示:

QSqlDatabase::database().driver()->subscribeToNotification("table_inserted");
QObject::connect(QSqlDatabase::database().driver(), SIGNAL(notification(const QString&)), /*handlerObjectPointer*/, SLOT(handleNotificationFunction(const QString&)));

这是我找到的部分答案: forum.qt.io

关于c++ - 如何在 PostgreSQL 中创建插入、删除或更新命令的信号并在 C++ 中处理它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com