Connecting and Creating Tables
Open a database and define schema.
What is sqlite3?
Python ships with the sqlite3 module in its standard library. It lets you work with SQLite, a lightweight, file-based SQL database that needs no separate server.
- No installation required
- The whole database lives in a single file
- Perfect for prototypes, tests, and small apps
import sqlite3
print(sqlite3.version)Connecting to a Database
Use sqlite3.connect() to open a database. If the file does not exist, SQLite creates it.
Passing ':memory:' creates a temporary in-memory database that disappears when the connection closes.
import sqlite3
conn = sqlite3.connect(':memory:')
print('Connected:', conn)
conn.close()All lessons in this course
- Connecting and Creating Tables
- Insert, Select, Update, Delete
- Parameterized Queries
- Transactions and Context Managers