How to catch events from MySQL?

Forum related to MySQL

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
TonyG
Fresh Boarder
Fresh Boarder
Posts: 8
Joined: 01.04.2006, 13:39

How to catch events from MySQL?

Post by TonyG »

Hello to all.

I'm using MySQL with ZEOS and I need to catch some events from MySQL. I would like to know if is it possible to know when a table have been modified (insert, delete or just update a record) for refreshing the data.

The application that I'm developing is going to be used by several users, so I need to know when the data are modified by other user in order to refresh the dbgrid.

Thanks in advance.

Anthony.
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Not supported by mysql as far as I know.

You could add triggers to your database table to monitor changes and insert/update records into a log table or a 'last changed' (one entry per monitored table) table. In your app you need to refresh a separate query against this log table every X seconds.

Mark
Image
TonyG
Fresh Boarder
Fresh Boarder
Posts: 8
Joined: 01.04.2006, 13:39

Post by TonyG »

Hello Mark.

Thanks a lot for your time and help.

You confirm what I thought: mysql not supports 'post event' as Interbase/Firebird does. Well, at the moment.

The solution that you tell me is ok, i'll do it.

Congratulations for you and all Zeos Dev Team. Go Zeoslib!!

Anthony.
tonci
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 23.07.2008, 19:54

Post by tonci »

Hi TonyG.

MySQL supports triggers for a long time now. Please see the manual.

DROP TABLE IF EXISTS Capital;
CREATE TABLE Capital
SELECT Country.Name AS Country, City.Name AS Capital,
City.Population
FROM Country, City
WHERE Country.Capital = City.ID;
delimiter //
CREATE TRIGGER Capital_bi
BEFORE INSERT
ON Capital
FOR EACH ROW
SET NEW.Population =
IF(NEW.Population < 0, 0, TRUNCATE(NEW.Population,-3));
//
delimiter ;

INSERT INTO Capital VALUES
('CountryA','CityA',-39),
('CountryB','CityB',123456);

Regards, Tonci
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Hi Tonci,

This is not what 'Firebird events' is about. Firebird events are signals the server sends to a client that requested to be notified. They can be fired by triggers or stored procedures.
Mysql only sends data to the client if that client first asks something. So you have to write a query to poll the server if there is some message waiting in a queue (which can be put there by triggers, inserts, ...).
I think technically this is more or less the same for Firebird, but there the mechanism of saving messages on the server until polled by the client and the automatic polling is build in. (I may be technically wrong about this, but I think the explanation will do to understand the difference.)

Mark
Image
tonci
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 23.07.2008, 19:54

Post by tonci »

Mark.

"I would like to know if is it possible to know when a table have been modified (insert, delete or just update a record) for refreshing the data."

I would say trigger would serve here just fine, possibly adding record to some temporary table which I can then pool from my code.

Anyway, I suck in most other servers but expensive ones.


Regards, Tonci.
Post Reply