Editor

Build a Contact Book

Advanced · 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

Build a mini contact book that performs all CRUD operations. Create a contacts table with name (TEXT) and phone (TEXT). Insert: Alice (555-0001), Bob (555-0002), Charlie (555-0003). Update Bob's phone to 555-9999. Delete Charlie. Then print all remaining contacts in the format name: phone, followed by a blank line and the total count in the format Total: N contacts.

import sqlite3
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
# Create table
# Insert contacts
# Update Bob's phone
# Delete Charlie
# Print remaining contacts and total count
conn.close()

More SQLite exercises

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