When developers think of databases, they usually think of PostgreSQL, MySQL, or MongoDB. They think of servers, ports, and complex installations.
But if you are building a lightweight app, a local script, or an AI tool on constrained hardware (like an Android phone via Termux), you don't need a database server.
You need SQLite.
SQLite is a C library that provides a lightweight disk-based database. It doesn't require a separate server process, and it allows you to access the database using a nonstandard variant of SQL. Best of all? It is built directly into Python.
Here is how to use it in 30 seconds.
1. The Built-in Module
Because sqlite3 is part of the Python Standard Library, you do not need to pip install anything. Just import it.
import sqlite3 2. Connecting and Creating a Table
When you connect to a database file, if the file doesn't exist, SQLite will automatically create it for you.
# Connect to the database (creates 'app.db' if it doesn't exist) conn = sqlite3.connect('app.db') cursor = conn.cursor() # Create a table cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE ) ''') # Save (commit) the changes conn.commit() 3. Inserting Data Safely
Never use f-strings to insert data into SQL! It leaves you open to SQL injection attacks. Always use the ? placeholder.
# Insert a new user cursor.execute(''' INSERT INTO users (name, email) VALUES (?, ?) ''', ('Ezenwa', 'ezenwa@example.com')) conn.commit() 4. Fetching Data
Once your data is saved, you can query it using fetchall() or fetchone().
cursor.execute('SELECT id, name, email FROM users') rows = cursor.fetchall() for row in rows: print(row) 5. Always Close the Connection
When you are done with your script, make sure to close the connection to free up system resources.
conn.close() Why use SQLite?
If you are building a mobile app, a CLI tool, or an AI script running on a local machine, SQLite is perfect. It is zero-configuration, incredibly fast for local data, and doesn't consume the memory that a full database server like PostgreSQL would.
Have you used SQLite in your Python projects? Let me know below!
