Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Database Access with SQLite | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 48 of 55

·Python

Database Access with SQLite

20 minutes

Course progress87%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Use `sqlite3.connect('file.db')` to open a database.
  • Parameterize queries (`?` placeholders) to avoid SQL injection.
  • Commit changes with `conn.commit()`.
  • Close connections with `conn.close()`.

Database Access with SQLite

SQLite is a self‑contained, serverless SQL database that ships with Python—perfect for small to medium applications.

import sqlite3

# Connect (creates file if it doesn't exist)
conn = sqlite3.connect('example.db')
cur = conn.cursor()

# Create a table
cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')

# Insert a row
cur.execute('INSERT INTO users (name) VALUES (?)', ('Alice',))
conn.commit()

# Query all rows
cur.execute('SELECT * FROM users')
print('Rows:', cur.fetchall())

conn.close()

For larger projects you might prefer an ORM like SQLAlchemy to avoid raw SQL.

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonBuilding a Simple REST APIContinue to lesson 49Introduction to DjangoBack to Python Fundamentals curriculum