Python Academy · Lesson

Connecting Python to Databases

Learn how to interact with databases in your web applications.

Lesson 4 of 411 steps

Connecting Python to Databases is a free Python Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

Connecting Python to Databases

Databases are essential for storing and retrieving data in web applications. Python provides libraries like sqlite3, SQLAlchemy, and PyMySQL to connect and interact with databases.

In this lesson, you’ll learn how to connect Python to a database and perform basic CRUD (Create, Read, Update, Delete) operations.

Connecting Python to Databases — illustration 1

Using SQLite with Python

SQLite is a lightweight, file-based database that comes bundled with Python. It’s ideal for small applications or testing purposes.

# Connecting to SQLite
import sqlite3

conn = sqlite3.connect('example.db')  # Creates or connects to a database file
print("Connected to SQLite")

Creating a Table

To create a table, use the CREATE TABLE SQL statement:

# Creating a table
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')
conn.commit()
print("Table created")

Inserting Data

You can insert data into a table using the INSERT INTO SQL statement:

# Inserting data
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 25))
conn.commit()
print("Data inserted")

Reading Data

You can retrieve data from a table using the SELECT SQL statement:

# Reading data
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
    print(row)

Updating Data

You can update existing records using the UPDATE SQL statement:

# Updating data
cursor.execute("UPDATE users SET age = ? WHERE name = ?", (30, "Alice"))
conn.commit()
print("Data updated")

Deleting Data

To delete records, use the DELETE SQL statement:

# Deleting data
cursor.execute("DELETE FROM users WHERE name = ?", ("Alice",))
conn.commit()
print("Data deleted")

Using SQLAlchemy

SQLAlchemy is a powerful library for database interactions in Python. It provides an Object Relational Mapper (ORM) for working with databases more easily.

# Installing SQLAlchemy
# pip install sqlalchemy

from sqlalchemy import create_engine
engine = create_engine('sqlite:///example.db')
print("Connected to SQLite with SQLAlchemy")

Common Mistakes with Databases

Here are some mistakes to avoid:

  • Forgetting to commit changes after executing write operations.
  • Not using parameterized queries to prevent SQL injection.
  • Hardcoding database connection strings in code.

What Did We Learn?

In this lesson, you learned:

  • How to connect Python to a SQLite database.
  • How to create, read, update, and delete records using SQL.
  • How to use SQLAlchemy for database interactions.

Great job! Let’s move to the next topic.

Connecting Python to Databases — illustration 11
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “Connecting Python to Databases” lesson free?

Yes — the full text of “Connecting Python to Databases” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “Connecting Python to Databases”?

Learn how to interact with databases in your web applications. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Connecting Python to Databases” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Basics of Web Frameworks
  2. Introduction to Flask
  3. Building a Simple Web App
  4. Connecting Python to Databases
← Back to Python Academy