Store data for a small app in SQLite instead of a server: one file, no database to run
For a small personal tool such as notes, inventory, or a reading log, a single SQLite file is enough. Python includes the sqlite3 module, so you do not need a database server or any extra package.
Notes, stock counts, a reading log. A small tool like this needs a place to keep its data, and a database server is more than it needs. A server runs in the background, takes memory, and needs its own setup and updates. On an 8GB PC that overhead is worth avoiding.
Why SQLite fits: SQLite stores the whole database in one ordinary file. Python's built-in sqlite3 module reads and writes that file directly, so nothing else has to be installed or kept running. Your data is a file you can copy, back up, and move.
This is a how-to guide. The steps were not benchmarked on the test machine; numbers are not included. The environment listed above is the one the steps target, not one that was confirmed on hardware.
When SQLite is the right choice
- One person, or a few people taking turns, using a tool on one PC
- Data that fits comfortably in a single file (thousands to hundreds of thousands of rows is typical for personal use)
- Writes that happen now and then, not many times per second
If several programs or users write at the same time all the time, or the data is shared across machines, a proper database server is the better choice. The limits are covered at the end of this article.
What you need
- Python 3.12 (any recent Python 3 works)
- Nothing else.
sqlite3is part of the standard library, so there is nopip installstep.
You can confirm the module is available with:
python3 -c "import sqlite3; print(sqlite3.sqlite_version)"
The command prints the SQLite library version, for example 3.45.1.
A minimal example: create, insert, select
The script below creates a table of notes, adds one note, and searches the titles. Save it as notes_app.py in its own folder. The database file notes.db is created in the same folder on the first run.
import sqlite3
from pathlib import Path
DB_PATH = Path(__file__).with_name("notes.db")
def connect():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db(conn):
conn.execute("""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
body TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
def add_note(conn, title, body):
conn.execute(
"INSERT INTO notes (title, body) VALUES (?, ?)",
(title, body),
)
conn.commit()
def search_notes(conn, keyword):
cur = conn.execute(
"SELECT id, title, created_at FROM notes WHERE title LIKE ? ORDER BY id DESC",
(f"%{keyword}%",),
)
return cur.fetchall()
if __name__ == "__main__":
conn = connect()
try:
init_db(conn)
add_note(conn, "Buy SSD", "Check the price of a 1TB drive")
for row in search_notes(conn, "SSD"):
print(row["id"], row["title"], row["created_at"])
finally:
conn.close()
Three points matter here:
CREATE TABLE IF NOT EXISTSmakes it safe to runinit_db()every time the app starts.- The
?placeholders pass values separately from the SQL text. A title likeBuy "SSD"; DROP TABLE notesis stored as plain text and does not change the query. conn.commit()saves the change. Without it, the insert is not written to the file.
Why not build SQL with string concatenation
Writing "... WHERE title = '" + keyword + "'" breaks as soon as the input contains a quote, and it lets input rewrite the query. Always pass values as the second argument to execute(), as in the example above. This applies even to a personal tool, because notes and imported text often contain quotes.
Backing up the database
The database is one file, so the simplest backup is to copy notes.db to another folder. Do this only when the app is closed. Copying a file while a program is writing to it can produce a copy that does not open cleanly.
If the app might be running, use the backup method instead. It copies the database through SQLite itself:
import sqlite3
src = sqlite3.connect("notes.db")
dst = sqlite3.connect("notes_backup.db")
src.backup(dst)
dst.close()
src.close()
Keep at least one copy on a different drive or in cloud storage. A backup on the same disk does not protect you from disk failure.
Limits to know about
- One writer at a time. SQLite lets only one connection write at a time. Python's default wait is 5 seconds. If two programs write at once for longer than that, you get
database is locked. - Not for many users over a network. Keeping the file on a network share and opening it from several PCs is a common way to corrupt data. Use a server database for shared use.
- Large data is slower. Very large tables and heavy searches need indexes and planning. That is beyond a personal tool, but it is worth knowing.
Notes
- The database file holds your data in plain form. Do not store passwords or personal information you would not keep in a text file, and keep the file out of shared folders.
- Check the file size and backups now and then. A growing database that nobody looks at is a common source of surprises.
- Keep the table definition in the code. If you change the columns later, plan a migration instead of editing the table by hand.
Summary
For a small personal app on an 8GB PC, SQLite gives you real SQL storage with no server to run. Use the sqlite3 module from the standard library, pass values with ? placeholders, and back up by copying the file while the app is closed. If many programs write at the same time, or the data is shared, move to a server database.