The moment your bot has to remember something between restarts, you need somewhere to put it. There are three reasonable answers and the wrong one only becomes obvious later, when the data is already there.
Choosing
| Option | Good for | Falls over when |
|---|---|---|
| A JSON file | Settings, small lists, a few dozen entries | Two writes overlap, or the process dies mid-write |
| SQLite | Most bots. Thousands of rows, real queries, one file | Several processes write at once |
| MySQL | Sharded bots, dashboards, data shared with a website | Nothing, but it is more to set up |
Writing the whole file on every change means a crash during a write leaves it truncated or empty. It is fine for config you edit by hand; it is a bad home for anything users generate. If you must, write to a temporary file and rename it over the original — a rename is atomic and cannot half-happen.
Moving from JSON to SQLite
- Add the library
better-sqlite3in Node, or the built-insqlite3module in Python. No server to run — the database is one file next to your code. - Create the table to match what you already store
One column per key you were writing. Keep the same names so the migration script is obvious.
- Write a one-off import
Read the JSON, insert the rows, keep the old file until you are sure. Deleting it a week later costs nothing; deleting it early costs everything.
- Include the database file in your backups
It is now the only copy of everything your bot knows.
The database file is the part of your bot you cannot rewrite from memory. Code you can push again from your laptop; a year of user settings you cannot.