sqlite3 module ships with every Python installation, so there is nothing to install. By the end of this page you will have a working Python script that creates an employee database, inserts records, queries them, and handles the connection safely. You will also know how to browse your .db file visually in VS Code.
The sqlite3 Module
sqlite3 is Python’s built-in interface to SQLite databases. It follows the DB-API 2.0 standard, which means the patterns you learn here apply to every other Python database driver (psycopg2 for PostgreSQL, mysql-connector for MySQL, etc.).
Core Concepts: Connection and Cursor
Working with SQLite in Python always follows the same four-step pattern.1
Connect
sqlite3.connect() opens (or creates) a .db file and returns a connection object. The connection represents your session with the database.":memory:" instead of a filename to create an in-memory database that lives only for the duration of your script — great for testing.2
Create a cursor
A cursor is the object you use to send SQL statements to the database and retrieve results.
3
Execute and commit
Use
cursor.execute() for single statements and cursor.executemany() for batch inserts. After INSERT, UPDATE, or DELETE you must call conn.commit() to persist the changes.4
Close the connection
Always close the connection when you are done to release the file lock.
The Context Manager Pattern (Recommended)
Usingwith sqlite3.connect(...) as a context manager automatically commits on success and rolls back on error. It also closes the connection when the with block exits — even if an exception is raised.
Fetching Results
After aSELECT query, use one of these methods to retrieve rows.
Complete Working Script
The following script creates thedepartment and employee tables, inserts data, and runs several queries. Run it once to create employees.db, then run it again — the IF NOT EXISTS guard prevents errors on subsequent runs.
Expected Output
Using Parameterized Queries
Never use Python string formatting to build SQL with user-supplied values. That opens your app to SQL injection. Instead, pass parameters using? placeholders.
- ❌ Unsafe (never do this)
- ✅ Safe (parameterized)
Getting Results as Dictionaries
By default,sqlite3 returns rows as plain tuples. Set conn.row_factory = sqlite3.Row to get dictionary-like objects where you can access columns by name.
Inspecting Your Database in VS Code
Install the SQLite Viewer extension to browse your.db file visually without writing any SQL.
1
Install the extension
Open VS Code → Extensions (
Ctrl+Shift+X / Cmd+Shift+X) → Search for “SQLite Viewer” by Florian Klampfer → Install.2
Open your .db file
In the VS Code Explorer, click on
employees.db. SQLite Viewer opens a table browser automatically.3
Browse tables and rows
You can see every table, click through rows, and verify that your Python script inserted data correctly — no terminal needed.
The SQLite Viewer extension is read-only. To run SQL queries interactively against your
.db file you can also install the SQLite extension by Alex Covizzi and use the Command Palette (Ctrl+Shift+P) → “SQLite: Open Database”.Database Schema Reference
Next: Querying Data with SELECT
Learn to filter, sort, aggregate, and group your employee data using the full power of the SELECT statement.