Skip to content

Connecting to SQLite using Python

SQLite is a lightweight, serverless database engine included with Python’s standard library. On ruachost.com, you can connect to SQLite databases using the built‑in sqlite3 module without installing any third‑party packages.

  • No external dependencies — sqlite3 is included in Python.
  • Perfect for lightweight applications, prototyping, and local development.
  • Stores data in a single portable .db file.
  • Supports SQL queries and transactions.
import sqlite3
# Connect to a database file (creates it if it doesn’t exist)
conn = sqlite3.connect('example.db')
curs = conn.cursor()
# Create a table
curs.execute("CREATE TABLE employees (firstname TEXT, lastname TEXT, title TEXT);")
# Insert data
curs.execute("INSERT INTO employees VALUES ('Kelly', 'Koe', 'Engineer');")
# Commit changes
conn.commit()
# Query data
curs.execute("SELECT firstname, lastname FROM employees;")
for firstname, lastname in curs.fetchall():
print(firstname, lastname)
conn.close()