# Database triggers

**URL:** <https://forum.xojo.com/t/database-triggers/72278>\
**Category:** General\
**Created:** [September 15, 2022, 9:39pm UTC](https://forum.xojo.com/t/database-triggers/72278 "2022-09-15T21:39:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertF](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@RobertF](https://forum.xojo.com/u/RobertF)\
**Post date:** [September 15, 2022, 9:39pm UTC](https://forum.xojo.com/t/database-triggers/72278/1 "2022-09-15T21:39:22Z")

</div>

Hello all,

Is there a way to get a trigger from a database insert (or change, etc.) so that some code will be executed from the triggering event? I am using MySQLCommunityServer.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [September 16, 2022, 12:04am UTC](https://forum.xojo.com/t/database-triggers/72278/2 "2022-09-16T00:04:22Z")

</div>

You mean you want the trigger to call your Xojo code? I don’t think that’s possible.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [September 16, 2022, 10:58am UTC](https://forum.xojo.com/t/database-triggers/72278/3 "2022-09-16T10:58:59Z")

</div>

maybe you can set a flag or timestamp for any table/field in the trigger and poll this in xojo app via timer.

PostgreSQL have some kind of message system in xojo.

---

<div class="post-metadata">

**Author:** ![RobertF](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@RobertF](https://forum.xojo.com/u/RobertF)\
**Post date:** [September 16, 2022, 3:09pm UTC](https://forum.xojo.com/t/database-triggers/72278/4 "2022-09-16T15:09:53Z")

</div>

Thank you, Tim Hare and MarkusR.

---

<div class="post-metadata">

**Author:** ![Arthur\_Couture](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@Arthur\_Couture](https://forum.xojo.com/u/Arthur_Couture)\
**Post date:** [September 16, 2022, 5:38pm UTC](https://forum.xojo.com/t/database-triggers/72278/5 "2022-09-16T17:38:44Z")

</div>

This example uses MariaDB, but there is a lot of overlap to MySQL:

> **[Motion Detection With Zoneminder Via SQL Triggers](https://community.openhab.org/t/motion-detection-with-zoneminder-via-sql-triggers/87638)**
>
> A TRIGGER is essentially a bit of SQL automatically executed by the database when operations occur within a table — table activity triggers execution. When ZoneMinder first detects motion, an event is recorded in the database. When motion is no...

From a security perspective, using Triggers to call external programs is highly discouraged, but depending on your environment, this may not be an issue.

---

<div class="post-metadata">

**Author:** ![RobertF](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@RobertF](https://forum.xojo.com/u/RobertF)\
**Post date:** [September 16, 2022, 5:54pm UTC](https://forum.xojo.com/t/database-triggers/72278/6 "2022-09-16T17:54:00Z")

</div>

Thank you, Arthur.

---

<div class="post-metadata">

**Author:** ![Lars\_Lehmann](https://forum.xojo.com/user_avatar/forum.xojo.com/lars_lehmann/32/11818_2.png) [@Lars\_Lehmann](https://forum.xojo.com/u/Lars_Lehmann)\
**Post date:** [September 17, 2022, 9:26pm UTC](https://forum.xojo.com/t/database-triggers/72278/7 "2022-09-17T21:26:38Z")

</div>

Also look at XojoORM. With this you’ll be able to define your own database events.

---

<div class="post-metadata">

**Author:** ![SteveP](https://forum.xojo.com/user_avatar/forum.xojo.com/stevep/32/6872_2.png) [@SteveP](https://forum.xojo.com/u/SteveP)\
**Post date:** [September 17, 2022, 10:51pm UTC](https://forum.xojo.com/t/database-triggers/72278/8 "2022-09-17T22:51:14Z")

</div>

Hi @Lars_Lehmann correct me if I’m wrong but using XojoORM for this purpose would essentially involve raising an event or similar before changes are made to the db ? IOW actual db triggers themselves would not invoke an event, correct ?

---

<div class="post-metadata">

**Author:** ![Maximilian\_Tyrtania](https://forum.xojo.com/user_avatar/forum.xojo.com/maximilian_tyrtania/32/294_2.png) [@Maximilian\_Tyrtania](https://forum.xojo.com/u/Maximilian_Tyrtania)\
**Post date:** [September 18, 2022, 10:00am UTC](https://forum.xojo.com/t/database-triggers/72278/9 "2022-09-18T10:00:57Z")

</div>

It actually is, and I’ve used it a lot (with PostgreSQL). The key is to have a persistant db connection in your Xojo app and have it listen for notifications, which you then send from within the trigger. On the Xojo side the ReceivedNotifications-Event is triggered (see: [PostgreSQLDatabase — Xojo documentation](https://documentation.xojo.com/api/databases/postgresqldatabase.html#postgresqldatabase-receivednotification)). Works like a charm.
