How to Open a SQLite Database in Python with sqlite3.connect
SQLite keeps a whole database in one file, and Python can work with it without installing anything: the sqlite3 module is part of the standard library. Every database task starts the same way, with sqlite3.connect. This guide opens a database file that doesn't exist yet, with real output from Python 3.15.2.
Syntax
import sqlite3
connection = sqlite3.connect(<database>)
# ... work with the database ...
connection.close()
| Part | What it means |
|---|---|
<database> | The path of the database file, as a string or a Path. Or ":memory:" for a database that lives only in memory |
connection | The Connection object. Every query goes through it |
connection.close() | Closes the database when you're done |
Python's sqlite3 documentation: "Open a connection to an SQLite database." About the database argument: "You can pass ":memory:" to create an SQLite database existing only in memory, and open a connection to it."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
The example opens library.db, a file that isn't there yet, and checks for the file before and after:
import sqlite3
from pathlib import Path
database = Path("library.db")
print("File exists before:", database.exists())
connection = sqlite3.connect(database)
print("File exists after:", database.exists())
print("SQLite version:", sqlite3.sqlite_version)
connection.close()

Path("library.db")names the file, in the folder you run the script from.database.exists()says whether it's there.sqlite3.connect(database)opens it. It accepts aPathas well as a string.sqlite3.sqlite_versionis the version of the SQLite library this Python uses.connection.close()closes the database.
Step 2: Run It
uv run python connect_db.py
File exists before: False
File exists after: True
SQLite version: 3.53.4

library.db didn't exist, so connect() created it: an empty database, ready for tables. Run the script again and the first line says True, because now connect() opens the file that's there. The version line shows the SQLite library that came with this Python, 3.53.4.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A misspelled file name | connect() creates a new, empty database with that name, and the first query fails with sqlite3.OperationalError: no such table | Check the path, or check Path.exists() before you connect |
| A folder in the path that doesn't exist | sqlite3.OperationalError: unable to open database file | Create the folder first |
Expecting a :memory: database to keep its tables | After close(), a new :memory: connection starts empty, and a query fails with no such table | Use a file path for data you want to keep |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A new, empty .db file appears in another folder | A relative path like library.db is relative to the folder you run the script from, not to the script's folder | Run the script from the folder that holds the database, or give the full path |
FAQ
Do I need to install SQLite?
No. The sqlite3 module is part of Python's standard library, and the SQLite library comes with Python: that's the 3.53.4 the script printed.
Which file extension should a SQLite database have?
Any name works. This guide uses library.db, a common choice.
When should I use ":memory:"?
For data you only need while the script runs, such as an example or a quick test: nothing is written to disk.
Conclusion
sqlite3.connect(database) found no library.db, so it created the file and opened it. The same call opens the file when it's already there.
Additional Resources
- sqlite3.connect — every argument of
connect(). - Connection.close — closing the connection, and committing first.
