Python and SQLite: Build Your Own Company Database in 30 Lines, Without a Server

For most Hong Kong SMEs, the "database" is really just an Excel file. That works for the first six months. Then the customer list grows from hundreds of rows to tens of thousands, and suddenly the file takes a minute to open, a colleague opening it at the same time gets read-only mode, and accounting and sales each keep their own version. In the meeting nobody is quite sure which number is real.

You do not need a server or a SaaS subscription before you can call it a database. SQLite, built into Python, is a complete relational database stored in a single file: nothing to install, no service to start. Below, about thirty lines of code turn an order CSV into something you can query instantly.

What SQLite is, and why it suits an SME

SQLite is an embedded database: the whole thing is one .db file that Python reads and writes directly, with no server, port or password in between. It requires zero installation (the sqlite3 module ships with Python), it speaks real SQL so anyone who knows SQL can use it, and a backup is just a file copy. With an index over a hundred thousand rows, a filtered query typically returns in well under a second. Be clear about its role, though: one program reading and writing one file, not dozens of people writing at once.

Step one: import from CSV

Say you have exported orders.csv from your POS, with columns order_id, date, customer, region, amount and paid (1 or 0 for settled):

import sqlite3, csv conn = sqlite3.connect("shop.db") cur = conn.cursor() cur.execute("CREATE TABLE IF NOT EXISTS orders (order_id TEXT PRIMARY KEY, date TEXT, customer TEXT, region TEXT, amount REAL, paid INTEGER)") with open("orders.csv", newline="", encoding="utf-8-sig") as f: rows = list(csv.DictReader(f)) cur.executemany( "INSERT OR REPLACE INTO orders VALUES (?, ?, ?, ?, ?, ?)", [(r["order_id"], r["date"], r["customer"], r["region"], float(r["amount"]), int(r["paid"])) for r in rows] ) cur.execute("CREATE INDEX IF NOT EXISTS idx_date ON orders(date)") cur.execute("CREATE INDEX IF NOT EXISTS idx_customer ON orders(customer)") conn.commit() print("Imported", len(rows), "rows") conn.close()

Line by line

  • connect("shop.db") — opens the database and creates the file if it does not exist.
  • PRIMARY KEY — order_id is the key, so the same order cannot enter twice.
  • date TEXT — dates are stored as text in a fixed YYYY-MM-DD shape, easy to compare later.
  • encoding="utf-8-sig" — Excel-exported CSVs carry an invisible BOM; without this the first column name turns to garbage.
  • executemany — writes the batch in one go, dozens of times faster; the question marks are placeholders, so you never build strings by hand.
  • CREATE INDEX — an index on date and customer is the table of contents; without it every query scans from the first row to the last.

Step two: ask questions in SQL

import sqlite3 conn = sqlite3.connect("shop.db") def query(sql): print("-" * 40) for row in conn.execute(sql): print(row) # 1. Revenue last month query("SELECT ROUND(SUM(amount), 2) FROM orders WHERE date LIKE '2026-09%'") # 2. Top five customers query("SELECT customer, ROUND(SUM(amount), 2) AS total FROM orders GROUP BY customer ORDER BY total DESC LIMIT 5") # 3. Unpaid orders above ten thousand query("SELECT order_id, customer, amount FROM orders WHERE paid = 0 AND amount > 10000 ORDER BY amount DESC") # 4. Performance by district query("SELECT region, COUNT(*), ROUND(SUM(amount), 2) FROM orders GROUP BY region ORDER BY 3 DESC") conn.close()

Four statements answer the four questions an owner asks most: how much did we do this month, which customers are worth the most, which invoices need chasing, which district performs best. Before, that meant filtering, copying to another sheet and adding up by hand. Now it is one statement, and the logic lives in the SQL rather than in whoever remembered to drag the formula down far enough.

The numbers: Excel versus SQLite

Rough measurements on an ordinary office laptop with 120,000 order rows:

  • Opening the file — Excel takes 40 to 90 seconds and stutters on the way; SQLite connects in about 0.05 seconds.
  • "Unpaid Kowloon orders in September" — about one to two minutes of manual filtering and checking; about 0.03 seconds in SQL.
  • Twelve monthly totals — Excel recalculates for a dozen seconds and can still miss a row; one GROUP BY returns all twelve in around 0.2 seconds.
  • Shared use — a second person opening the Excel file gets read-only; SQLite allows many readers, though writers take turns.

When SQLite is the wrong tool

In these situations, go straight to PostgreSQL, MySQL or a cloud service:

  • Several people writing at once — three shops taking orders simultaneously will queue on a file lock.
  • Per-user permissions — sales seeing only their own accounts needs real database accounts.
  • Direct queries from a website or app — put an API layer in front instead.
  • Real-time cross-department sync — a single file on a shared drive always raises the question of which copy is current.

Backups and housekeeping

A backup is a copy of shop.db — no export needed. Copy it to an external drive or cloud folder at the end of each day with the date in the filename, such as shop_20261023.db, and keep the last thirty. After heavy deletions or edits, run VACUUM once to reclaim space so the file shrinks and queries speed up a little. And form one habit: back up before any change — a DELETE with the wrong WHERE clause has no undo button.

Summary

Python Tutorial: setting up a database does not have to mean buying a server or signing a year-long SaaS contract. With Python's built-in sqlite3, one file and thirty lines give you table creation, import, indexing and querying — enough for most internal SME data work. A sensible order of adoption: import two or three months of CSV and run it, confirm the columns and date formats line up, rewrite your three most frequent reports in SQL, and only then consider scheduling automation. When you catch yourself asking "could we add one more column?", you are already genuinely using it.