Python:如何获取有关MySQL数据库更改的通知? [英] python: how to get notifications for mysql database changes?

查看:150
本文介绍了Python:如何获取有关MySQL数据库更改的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,有没有办法通知MySQL数据库中的特定表已更改?

In Python, is there a way to get notified that a specific table in a MySQL database has changed?

推荐答案

从理论上讲是可行的,但我不建议这样做:

It's theoretically possible but I wouldn't recommend it:

本质上,您在表上有一个触发器,该触发器调用以某种方式与您的Python应用程序通信的UDF.

Essentially you have a trigger on the the table the calls a UDF which communicates with your Python app in some way.

陷阱包括发生错误时会发生什么?

Pitfalls include what happens if there's an error?

如果阻止了该怎么办?理想情况下,触发器内部发生的任何事情都应该是即时的.

What if it blocks? Anything that happens inside a trigger should ideally be near-instant.

如果它在回滚的事务中怎么办?

What if it's inside a transaction that gets rolled back?

我确定还有很多其他我没有想到的问题.

I'm sure there are many other problems that I haven't thought of as well.

如果可能的话,一种更好的方法是让您的数据访问层通知您应用程序的其余部分.如果您正在寻找控件之外的程序何时修改数据库,那么您可能不走运.

A better way if possible is to have your data access layer notify the rest of your app. If you're looking for when a program outside your control modifies the database, then you may be out of luck.

另一种较不理想,但比从触发器内调用另一个程序更好的imo是,设置某种"LastModified"表,该表将由带有触发器的触发器更新.然后在您的应用程序中,只需检查该日期时间是否大于您上次检查的日期时间即可.

Another way that's less ideal but imo better than calling an another program from within a trigger is to set some kind of "LastModified" table that gets updated by triggers with triggers. Then in your app just check whether that datetime is greater than when you last checked.

这篇关于Python:如何获取有关MySQL数据库更改的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆