15
Lesson 39 of 44

Databases with SQLite

Learn to store and query data with a relational database without installing anything. In this lesson you will use the sqlite3 module to create tables and insert rows, query with SELECT (WHERE, ORDER BY, LIMIT), modify data with UPDATE and DELETE using safe parameters, group operations into transactions with commit and rollback, protect the data with constraints, and summarize and combine tables with GROUP BY, HAVING and JOIN.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Databases with sqlite3: first steps

Almost every application stores data: users, orders, grades, messages. A database stores them in an orderly way and lets you…

Theory
Read more

Querying data with SELECT

SELECT is the SQL command you will use the most. The examples in this article use this products table:

Theory
Read more

Parameters, UPDATE and DELETE

So far you have read data. To change it, INSERT , UPDATE and DELETE are used. But first, the most important security rule when…

Theory
Read more

Transactions and constraints

A transaction groups several operations so that all or none are applied. It is what prevents a transfer from taking money out of…

Theory
Read more

Grouping and joining tables

The power of SQL lies in summarizing data and combining tables without writing loops in Python.

Theory
Read more

Practice exercises 10

Contact book with SQLite

Create a Python program that stores contacts in an SQL table and queries them. Use an in-memory database with the sqlite3 module…

Exercise Easy
Solve it

Filter by price

Create a Python program that selects, with a WHERE query, the products that cost at least a minimum price. Use an in-memory…

Exercise Easy
Solve it

Total per category

Create a Python program that groups sales by category with GROUP BY and the aggregate functions SUM and COUNT . Use an in-memory…

Exercise Medium
Solve it

Update and delete

Create a Python program that modifies an inventory with UPDATE and DELETE , and shows how many rows each operation affects with…

Exercise Medium
Solve it

Safe search

Create a Python program that searches users by part of their name with LIKE without concatenating text into the SQL : always use…

Exercise Medium
Solve it

Paged ranking

Create a Python program that shows a ranking in pages using LIMIT and OFFSET in the query. Use an in-memory database with the…

Exercise Medium
Solve it

Transfers with rollback

Create a Python program that makes transfers between accounts inside a transaction : if something fails, no change is applied…

Exercise Hard
Solve it

Customers and orders

Create a Python program that relates two tables with LEFT JOIN and computes each customer's total, even if they have no orders…

Exercise Hard
Solve it

Statistics per subject

Create a Python program that computes statistics per subject with AVG , MAX , MIN and GROUP BY , and discards small groups with…

Exercise Hard
Solve it

Integrity constraints

Create a Python program that lets the database itself reject invalid data through constraints, catching sqlite3.IntegrityError …

Exercise Hard
Solve it