Skip to main content

How to Open a SQLite Database in Python with sqlite3.connect

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import sqlite3

connection = sqlite3.connect(<database>)
# ... work with the database ...
connection.close()
PartWhat 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
connectionThe 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:

connect_db.py
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()

The connect_db.py script in a macOS-style code window: it checks whether library.db exists, opens it with sqlite3.connect, checks again, prints the SQLite version and closes the connection.

  • 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 a Path as well as a string.
  • sqlite3.sqlite_version is the version of the SQLite library this Python uses.
  • connection.close() closes the database.

Step 2: Run It​

Terminal
uv run python connect_db.py
Output
File exists before: False
File exists after: True
SQLite version: 3.53.4

The output in a terminal: File exists before: False, File exists after: True, and 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​

MistakeWhat happensDo this instead
A misspelled file nameconnect() creates a new, empty database with that name, and the first query fails with sqlite3.OperationalError: no such tableCheck the path, or check Path.exists() before you connect
A folder in the path that doesn't existsqlite3.OperationalError: unable to open database fileCreate the folder first
Expecting a :memory: database to keep its tablesAfter close(), a new :memory: connection starts empty, and a query fails with no such tableUse a file path for data you want to keep

Troubleshooting​

SymptomLikely causeFix
A new, empty .db file appears in another folderA relative path like library.db is relative to the folder you run the script from, not to the script's folderRun 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​