Contact Book - Python Project Using MySql

Project Overview

A contact book is a tool for saving a contact’s details, such as name, address, phone number, and email address.

In this project, you will build a Contact Book application that allows users to:
1. Add new contact records.
2. Search for contacts by name.
3. Display all contact records.
4. Delete contact records.
5. Modify contact records.

Prerequisites

Before you begin, make sure you have the following installed:

Python: Download Python
MySQL: Download MySQL
mysql-connector-python: install it using pip

pip install mysql-connector-python

Setting Up the Database

To get started, create a MySQL database to store contact information. Here's how you can do it:
1. Open your MySQL command-line client.
2. Execute the following SQL commands to create a database named "contact":

CREATE DATABASE IF NOT EXISTS contact;

Building the Application

We are providing a simplified version of the contact book. You can enhance and customize it as you learn more.

Program (contact-book.py)

import mysql.connector
import time

db = mysql.connector.connect(
    host="localhost",
    user="root",
    password="1234",
    database="contact"
)

cursor = db.cursor()

cursor.execute("""
    CREATE TABLE IF NOT EXISTS book (
        name char(30) primary key,
        address char(100),
        mobile char(15),
        email char(30)
    );
""")


def intro():
    print("=" * 80)
    print("{:^80s}".format("CONTACT"))
    print("{:^80s}".format("BOOK"))
    print("{:^80s}".format("PROJECT"))
    print("{:^80s}".format("MADE BY: PyForSchool.com"))
    print("=" * 80)
    print()
    time.sleep(2)


def create_record():
    name = input("Enter name: ")
    address = input("Enter address: ")
    mobile = input("Enter mobile: ")
    email = input("Enter email: ")
    sql = "INSERT INTO book(name,address,mobile,email) VALUES (%s,%s,%s,%s)"
    record = (name, address, mobile, email)
    cursor.execute(sql, record)
    db.commit()
    print("Record Entered Successfully\n")


def search(name):
    sql = "SELECT * FROM book WHERE name = %s"
    value = (name,)
    cursor.execute(sql, value)
    record = cursor.fetchone()
    if record is None:
        print("No such record exists")
    else:
        print('Name:', record[0])
        print('Address:', record[1])
        print('Mobile:', record[2])
        print('E-mail:', record[3])


def display_all():
    cursor.execute("SELECT * FROM book")
    print('{0:20}{1:30}{2:15}{3:30}'.format('NAME', 'ADDRESS', 'MOBILE NO', 'E-MAIL'))
    for record in cursor:
        print('{0:20}{1:30}{2:15}{3:30}'.format(record[0], record[1], record[2], record[3]))


def delete_record(name):
    sql = "DELETE FROM book WHERE name = %s"
    value = (name,)
    cursor.execute(sql, value)
    db.commit()
    if cursor.rowcount == 0:
        print("Record not found")
    else:
        print("Record deleted successfully")


def modify_record(name):
    sql = "SELECT * FROM book WHERE name = %s"
    value = (name,)
    cursor.execute(sql, value)
    record = cursor.fetchone()
    if record is None:
        print("No such record exists")
    else:
        while True:
            print("\nPress the option you want to edit: ")
            print("1. Name")
            print("2. Address")
            print("3. Mobile")
            print("4. BACK")
            print()
            ch = int(input("Select Your Option (1-4): "))
            if ch == 1:
                new_name = input("Enter new name: ")
                sql = "UPDATE book SET name = %s WHERE name = %s"
                values = (new_name, name)
                cursor.execute(sql, values)
                db.commit()
                print(cursor.rowcount, "record updated successfully")
            elif ch == 2:
                new_address = input("Enter new address: ")
                sql = "UPDATE book SET address = %s WHERE name = %s"
                values = (new_address, name)
                cursor.execute(sql, values)
                db.commit()
                print(cursor.rowcount, "record updated successfully")
            elif ch == 3:
                new_mobile = input("Enter new mobile : ")
                sql = "UPDATE book SET mobile = %s WHERE name = %s"
                values = (new_mobile, name)
                cursor.execute(sql, values)
                db.commit()
                print(cursor.rowcount, "record updated successfully")
            elif ch == 4:
                break
            else:
                print("Invalid choice !!!\n")


def main():
    intro()
    while True:
        print("\nMAIN MENU ")
        print("1. ADD NEW RECORD")
        print("2. SEARCH RECORD")
        print("3. DISPLAY ALL RECORDS")
        print("4. DELETE RECORD")
        print("5. MODIFY RECORD")
        print("6. EXIT")
        print()
        ch = int(input("Select Your Option (1-6): "))
        print()
        if ch == 1:
            print("ADD NEW RECORD")
            create_record()
        elif ch == 2:
            print("SEARCH RECORD BY NAME")
            name = input("Enter name: ")
            search(name)
        elif ch == 3:
            print("DISPLAY ALL RECORDS")
            display_all()
        elif ch == 4:
            print("DELETE RECORD")
            name = input("Enter name: ")
            delete_record(name)
        elif ch == 5:
            print("MODIFY RECORD")
            name = input("Enter name: ")
            modify_record(name)
        elif ch == 6:
            print("Thanks for using Contact Book")
            db.close()
            break
        else:
            print("Invalid choice")


main()

Output


Learning and Customization

This Contact Book project is an excellent opportunity to learn and practice Python and database management. As you become more proficient, consider expanding the project by adding features like user authentication, more advanced search and filter options.

Score Excellent Marks in CBSE Class 12 Computer Science

Do you want to excel in your exams and gain a deep understanding of Python programming

Look no further! We present to you "PyForSchool's Computer Science with Python Class XII 2023-24 [Revision Notes & Question Bank]"

An outstanding 4.5 out of 5-star rating on Amazon!

Buy Now