Does MySQL allow callbacks in C, so when the change happens, can I get a notification?

Does MySQL allow C callbacks so that when a change is made to a database, such as an insert that is being executed by another program or by a user on the command line, I can receive notifications?

I assume this is not the case, because mysqlclient is a library, not a working thread. But I can also ask.

+3
source share
4 answers

Create such a trigger.

DELIMITER $$ CREATE TRIGGER ad_mytable_each AFTER DELETE ON MyTable FOR EACH ROW BEGIN #write code that trigger After delete (hence the "ad_" prefix) #For table MyTable (The _MyTable_ middle) #On each row that gets inserted (_each suffix) # #You can see the old delete values by accesing the "old" virtual table. INSERT INTO log VALUES (old.id, 'MyTable', old.field1, old.field2, now()); END$$ DELIMITER ; 

There are triggers for INSERT , DELETE , UPDATE
And they can trigger a BEFORE or AFTER action.
A trigger of a BEFORE action can cancel an action, causing an error, for example.

 CREATE TRIGGER bd_mytable_each BEFORE DELETE ON MyTable FOR EACH ROW BEGIN #write code that trigger Before delete (hence the "db_" prefix) declare DoError Boolean; SET DoError = 0; IF old.id = 1 THEN SET DoError = 1; END IF; IF (DoError = 1) THEN SELECT * FROM Table_that_does_not_exist_to_force_error; #seriously this example is in the manual. END$$ DELIMITER ; 

This will prevent the deletion of record 1.

A before UPDATE A trigger can even change updated values.

 CREATE TRIGGER bu_mytable_each BEFORE UPDATE ON MyTable FOR EACH ROW BEGIN IF new.text = 'Doon sucks' THEN SET new.text = 'Doon rules'; END$$ DELIMITER ; 

I hope you will be happy.

+4
source

MySQL triggers allow you to connect to insert / update / delete requests and do something extra. For example, you can register them in a separate table.

+2
source

Well, you can attach a trigger to a user-defined function and call an external program, which then tells you the code.

http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html#qandaitem-B-5-1-10

+1
source

You can use triggers in combination with UDFs (user-defined functions), so that the corresponding action in the database is performed by a trigger that calls the C / C ++ function.

Just think that this mechanism runs your code inside the mysql server process, and not on the client side.

+1
source

Source: https://habr.com/ru/post/896074/


All Articles