Day 9 · Day 9: Automation & Databases
Contact Book
Build a Contact Book application that stores contacts in a SQLite database, supporting adding, viewing, searching, updating, and deleting contacts with persistent storage.
Requirements
1. Use the sqlite3 module to create a contacts.db database with a contacts table (id, name, phone, email).
2. Implement add_contact() to insert a new contact using parameterized queries.
3. Implement view_contacts() to display all contacts sorted by name.
4. Implement search_contact() to find contacts by name (partial match using LIKE).
5. Implement update_contact() to change a contact's phone or email.
6. Implement delete_contact() to remove a contact by id.
7. Build a menu-driven loop tying all operations together.
8. Ensure the database and table are created automatically if they don't exist.
2. Implement add_contact() to insert a new contact using parameterized queries.
3. Implement view_contacts() to display all contacts sorted by name.
4. Implement search_contact() to find contacts by name (partial match using LIKE).
5. Implement update_contact() to change a contact's phone or email.
6. Implement delete_contact() to remove a contact by id.
7. Build a menu-driven loop tying all operations together.
8. Ensure the database and table are created automatically if they don't exist.
import sqlite3
DB_NAME = "contacts.db"
def init_db():
with sqlite3.connect(DB_NAME) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT,
email TEXT
)
""")
conn.commit()
def add_contact(name, phone, email):
with sqlite3.connect(DB_NAME) as conn:
conn.execute(
"INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)",
(name, phone, email)
)
conn.commit()
print(f"Added contact: {name}")
def view_contacts():
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.execute("SELECT id, name, phone, email FROM contacts ORDER BY name")
rows = cursor.fetchall()
if not rows:
print("No contacts found.")
return
for row in rows:
print(f"{row[0]}. {row[1]} | Phone: {row[2]} | Email: {row[3]}")
def search_contact(name):
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.execute(
"SELECT id, name, phone, email FROM contacts WHERE name LIKE ?",
(f"%{name}%",)
)
rows = cursor.fetchall()
if not rows:
print("No matching contacts.")
return
for row in rows:
print(f"{row[0]}. {row[1]} | Phone: {row[2]} | Email: {row[3]}")
def update_contact(contact_id, phone, email):
with sqlite3.connect(DB_NAME) as conn:
conn.execute(
"UPDATE contacts SET phone = ?, email = ? WHERE id = ?",
(phone, email, contact_id)
)
conn.commit()
print("Contact updated.")
def delete_contact(contact_id):
with sqlite3.connect(DB_NAME) as conn:
conn.execute("DELETE FROM contacts WHERE id = ?", (contact_id,))
conn.commit()
print("Contact deleted.")
def main():
init_db()
while True:
print("\n--- Contact Book ---")
print("1. Add Contact")
print("2. View All Contacts")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Exit")
choice = input("Choose an option: ")
if choice == "1":
name = input("Name: ")
phone = input("Phone: ")
email = input("Email: ")
add_contact(name, phone, email)
elif choice == "2":
view_contacts()
elif choice == "3":
name = input("Search name: ")
search_contact(name)
elif choice == "4":
contact_id = int(input("Contact ID: "))
phone = input("New phone: ")
email = input("New email: ")
update_contact(contact_id, phone, email)
elif choice == "5":
contact_id = int(input("Contact ID: "))
delete_contact(contact_id)
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid option.")
if __name__ == "__main__":
main()
50 XP on completion
Back to Day 9
Your Code
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to mark this project complete and earn XP.