9

Some devices are asynchronously storing values on a common remote MySQL database server.

I would like to write a supervisor app in Python (and possibly SQLAlchemy) to recognize the external INSERT events on the database and act upon the last rows' data. This is to avoid a long manual test to see if every table is being updated regularly or a logger crashed.

Can somebody just tell me where to search online this kind of info and, even better, an example?

EDIT

I already read all tables periodically using a datetime primary key ({date_time}), loading the last row of each table, and comparing to the previous values:

SELECT * FROM table ORDER BY date_time DESC LIMIT 1

but it looks very cumbersome and doesn't guarantee that I don't lose some rows between successive database checks.

The engine is an old version of INNODB that I cannot upgrade: I cannot use the UPDATE field in schema because it simply doesn't work.

To reword my question: How to listen any database event with a daemon-like Python application (sleeping thread) and wake up only when something happens?

I want also to avoid SQL triggers because this would be just too heavy to manage: tables are in hundreds and they are added/removed very often according to the active loggers.

I gave a look to SQLAlchemy but all reference I could find, if I don't misunderstood it, are decorators to act on INSERTs made by SQLAlchemy's itself. I didn't find anything about external changes to the database.

About the example request: I am not interested in a copy-and-paste, because first I want to understand how stuff works. I prefer (even incomplete) examples because SQLAlchemy documentation is far too deep for my knowledge and I simply cannot put the pieces together.

Alex Poca
  • 2,406
  • 4
  • 25
  • 47
  • Why not store timestamp with each record, and then periodically `SELECT` the timestamp of the latest record to see whether it's within your desired threshold? – eggyal Aug 26 '16 at 08:35
  • @eggyal, I understood that I didn't explain enough my need. I added an edit to the post. Reading the timestamp is what I do already, sorry I made you lose time. – Alex Poca Aug 26 '16 at 11:08
  • This cannot be done. You cannot react to changes in MySQL without using triggers. You *can* use the replication mechanism to listen to changes but this is too complicated for your simple use case. Polling for the newest timestamp is a perfectly valid solution and should work just as well. – univerio Aug 26 '16 at 18:34
  • Thank you @univerio. Just to know that it is not possible without triggers is a good answer to avoid a lot of useless search (as I did). About the downvotes: they bother me because usually there are no hints on WHY you get one. In my case I have been a programmer for almost 30 years (spent mostly on C and some forgotten languages). When I make a question it means I already made some search. I am used to strip down to leave the most meaningful part. Reading back what I wrote before the edit I cannot understand what is wrong with it... – Alex Poca Aug 26 '16 at 19:45
  • Please @univerio, move your comment to answers so I can close the issue. – Alex Poca Aug 27 '16 at 09:38

0 Answers0