mysql - 如何观察数据库中某个表是否增加了一条记录
黄舟
黄舟 2017-04-17 11:28:29
0
3
886

如题,数据库用的是MySQL。其中某个表一直以周期性添加一条数据。
目前用的是轮询的方式。
题主想知道数据库是否支持类似于Java中观察者模式,当数据库添加记录后,就会自动触发某个自定义函数?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
小葫芦

The answers below are all based on the large amount of data in the main table.

Method 1

(1) Use mysql trigger
(2) mysql can run external scripts (may be related to user permissions and platform), and trigger back-end programs through scripts

Method 2

(1) Use mysql trigger
(2) After triggering the data, write the data to the memory table
(3) The back-end program polls the memory table. After the processing is completed, the polled data is deleted to prevent the memory table from exploding

洪涛

The best solution to your idea should be Hook to make a hook for MySQL and execute the hook program when the database changes. However, after searching, there seems to be no good ideas for directly adding Hooks to the database.
The only feasible way is to use Git to manage your MySQL database, and call Hook through Git when the database file changes. Although it is feasible, it feels a little geeky.

So it seems that the simpler idea is to think from your program. When the program executes the insert() function in the database operation class, it executes a certain Hook函数. That is to say, do not add hooks to the database, but add hooks to the database operation class of the program.

小葫芦

触发器Can it meet your needs? ?

Triggered when insert is executed. .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template