Reputation: 1517
I'm trying to find the best way to get notified by the database when there are insert, delete or update commands in PostgreSQL.
My goal is to handle changes in the database as soon as they happen.
How can I do that?
Upvotes: 0
Views: 920
Reputation: 1517
I've found a good way of doing that in Qt.
First, you need to create rules to notify you that updates had happened:
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;
Then, you can use Qt to receive each notification ("table_inserted", "table_updated", "table_deleted") as follows:
QSqlDatabase::database().driver()->subscribeToNotification("table_inserted");
QObject::connect(QSqlDatabase::database().driver(), SIGNAL(notification(const QString&)), /*handlerObjectPointer*/, SLOT(handleNotificationFunction(const QString&)));
Here is where I found part of the answer: forum.qt.io
Upvotes: 4