Toggle swich / write to DB

Hello,

I would like to have a toggle switch that will write some value to a database, and also set the toggle switch status to the matching value in the DB.

The DB is a MariaDB (externally)

I have searched a lot already, but cannot find how to approach this.

Create an automation the trigger off the switch state, write a value to an input_text and then record thst vslue