1
RYAN
INTERNATIONAL
SCHOOL
SUBMITTED BY: Rudra Pratap Singh
CLASS: XII-B
2
INDEX
S no. TITLE
PAGE
3
1 CERTIFICATE
4
2 ACKNOWLEDGEMENT
5
3 OBJECTIVE
6
4 PYTHON CODE
13
5 OUTPUTS
3
CERTIFICATE
This is to certify that, Rudra Pratap, a student of class
12th(science) has successfully completed his project on
the topic “CONTACT BOOK” under the guidance of
subject teacher Ms Shubhra Gupta during the year
2023-24 at Ryan International School, Sohna Road in
partial fulfilment of Computer science practical
examination conducted by CBSE.
4
ACKNOWLEDGMENT
I would like to extend my sincere and heartfelt
obligation towards all those who have helped me in
making this project. Without their active guidance, help,
cooperation, and encouragement, I would not have been
able to present the project on time.
I am extremely thankful and pay my sincere gratitude to
my teacher Ms. Shubhra Gupta for her valuable
guidance and support in the completion of this project.
I also acknowledge with a deep sense of reverence, my
gratitude towards my parents, other faculty members of
the school, and friends for their valuable suggestions
given to me in completing the project.
5
Objective
The Python-based Contact Book program allows users to
efficiently manage contact records by adding new entries,
searching by name, displaying a comprehensive list of all
contacts, deleting outdated records, and modifying existing
details. Implemented with a user-friendly command-line
interface, the application ensures input validation and data
integrity, offering a practical solution for organizing and
maintaining contact information.
6
PYTHON CODE
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"))
7
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")
8
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):
9
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: ")
10
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")
11
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
12
else:
print("Invalid choice")
main()
13
OUTPUTS
14