Python2 minutes to read

Can a hosted Python bot keep its settings in SQLite?

Use a persistent SQLite file for a single Python bot, verify a setting after restart, and decide when a second application needs a database server.

Store the database in the deployment files

Yes. A single Python bot on Bothosting can store settings in a SQLite file on its deployment volume. Python’s sqlite3 module opens the database in the process, so this uses the bot’s existing deployment and storage allocation. Put the database outside temporary directories and frontend public folders. A live database contains user data and should stay out of your Git repository. Python’s sqlite3 reference.

For a bot with its entry script at the project root, this example puts the database beside that script under data. Building the path from __file__ makes the location predictable even if you later change the working directory.

Open the same file on every start
from pathlib import Path
import sqlite3

data = Path(__file__).resolve().parent / "data"
data.mkdir(exist_ok=True)
db = sqlite3.connect(data / "settings.sqlite3")
db.execute("CREATE TABLE IF NOT EXISTS settings (name TEXT PRIMARY KEY, value TEXT)")

Test the write across a restart

In a test command, save the value Europe/Rome for a setting named timezone. Use bound parameters for values and commit the transaction. Have a separate read command look up that key and display the result. Restart the bot and run only the read command. Seeing the same timezone confirms the data survived a fresh process.

If the application opens an empty table after a restart, inspect the file path and make sure your startup code does not replace it with a blank copy. If the file path stayed the same, check the transaction commit.

Plan for recovery and a second application

For a simple file backup, stop the bot cleanly so writes have finished, back up the deployment, and verify the database can be opened after a restore. If you need an online database copy, Python’s SQLite connection also exposes a backup operation. Back up deployment files.

If a separate website and bot both need to query the same records, their individual deployment volumes have separate files. Consider a database deployment that both applications can reach through a driver and budget a slot for it. Compare hosted database engines.