Editor

Manage an Inventory

Intermediate · Write the code · from Python SQLite: Build and Query Databases Without a Server

From the lesson: Python SQLite: Build and Query Databases Without a Server

Every app needs to store data somewhere. A to-do list needs to remember your tasks. A contacts app needs to save names and phone numbers. You could write everything to a text file, but what happens when you need to find all contacts in a specific city, or delete tasks older than a week?

Read the lesson

Create an inventory table with columns item (TEXT) and quantity (INTEGER). Insert: Apples (50), Bananas (30), Oranges (0), Grapes (15). Update Bananas to quantity 45. Delete all items with quantity 0. Then print the remaining items in the format item: quantity.

import sqlite3
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
# Create table and insert items
# Update Bananas quantity to 45
# Delete items with quantity 0
# Print remaining items
conn.close()

More SQLite exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz