Advertisement
DrAungWinHtut

addressbook.py

Feb 21st, 2023
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import pymysql
  2.  
  3. # Function to create a connection to the database
  4. def connect_to_database(db_name):
  5.     try:
  6.         conn = pymysql.connect(host='localhost',
  7.                                user='root',
  8.                                password='',
  9.                                database=db_name)
  10.         print("Connected to the database successfully")
  11.         return conn
  12.     except Exception as e:
  13.         print("Error connecting to the database: ", e)
  14.  
  15. # Function to create a new table for the address book
  16. def create_table(conn):
  17.     try:
  18.         cursor = conn.cursor()
  19.         cursor.execute("CREATE TABLE IF NOT EXISTS contacts (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), email VARCHAR(255), phone VARCHAR(255))")
  20.         conn.commit()
  21.         print("Table created successfully")
  22.     except Exception as e:
  23.         conn.rollback()
  24.         print("Error creating table: ", e)
  25.  
  26. # Function to insert a new contact into the address book
  27. def add_contact(conn, name, email, phone):
  28.     try:
  29.         cursor = conn.cursor()
  30.         cursor.execute("INSERT INTO contacts (name, email, phone) VALUES (%s, %s, %s)", (name, email, phone))
  31.         conn.commit()
  32.         print("Contact added successfully")
  33.     except Exception as e:
  34.         conn.rollback()
  35.         print("Error adding contact: ", e)
  36.  
  37. # Function to view all contacts in the address book
  38. def view_contacts(conn):
  39.     try:
  40.         cursor = conn.cursor()
  41.         cursor.execute("SELECT * FROM contacts")
  42.         result = cursor.fetchall()
  43.         print("Contacts:")
  44.         for row in result:
  45.             print(row)
  46.     except Exception as e:
  47.         conn.rollback()
  48.         print("Error viewing contacts: ", e)
  49.  
  50. # Function to delete a contact from the address book
  51. def delete_contact(conn, id):
  52.     try:
  53.         cursor = conn.cursor()
  54.         cursor.execute("DELETE FROM contacts WHERE id=%s", (id,))
  55.         conn.commit()
  56.         print("Contact deleted successfully")
  57.     except Exception as e:
  58.         conn.rollback()
  59.         print("Error deleting contact: ", e)
  60.  
  61. # Connect to the database
  62. conn = connect_to_database("addressbook")
  63.  
  64. # Create the contacts table if it does not already exist
  65. create_table(conn)
  66.  
  67. # Prompt the user to add, view, or delete contacts
  68. while True:
  69.     print("Enter 'add' to add a new contact")
  70.     print("Enter 'view' to view all contacts")
  71.     print("Enter 'delete' to delete a contact")
  72.     print("Enter 'quit' to exit")
  73.  
  74.     choice = input("Choice: ")
  75.  
  76.     if choice == 'add':
  77.         name = input("Enter name: ")
  78.         email = input("Enter email: ")
  79.         phone = input("Enter phone: ")
  80.         add_contact(conn, name, email, phone)
  81.     elif choice == 'view':
  82.         view_contacts(conn)
  83.     elif choice == 'delete':
  84.         id = input("Enter ID of contact to delete: ")
  85.         delete_contact(conn, id)
  86.     elif choice == 'quit':
  87.         break
  88.     else:
  89.         print("Invalid choice")
  90.  
  91. # Close the connection to the database
  92. conn.close()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement