def find_contact(): name = input('\nEnter the name of the contact you wish to find: ') search_results = cursor.execute("SELECT * FROM contacts WHERE name = (?)", (name, )).fetchall() list_of_names = [] for i in range(0, search_results.__len__()): list_of_names.append(search_results[i][0]) if list_of_names.__contains__(name): if list_of_names.count(name) > 1: print(">> Multiple {}s were found.\n".format(name)) for item in search_results: print(item) proceed() else: target_number = cursor.execute( "SELECT number FROM contacts WHERE name = (?)", (name, )).fetchone()[0] print(">> {} has been found. {}'s number is {}.".format( name, name, target_number)) proceed() else: print(">> {} not found.".format(name)) proceed()
def create_table(): try: cursor.execute("""CREATE TABLE contacts ( name text, number integer) """) except sqlite3.OperationalError: pass
def add_contact(): name = input('\nEnter contact name: ') number = int(input("Enter {}'s number: ".format(name))) new_contact = Contacts(name, number) cursor.execute("INSERT INTO contacts VALUES (?, ?)", (new_contact.name, new_contact.number)) conn.commit() view_contacts()
def send_message(): print("\n===============" "\nWelcome to SMS." "\n===============") to = input("\nTo: ") search_results = cursor.execute("SELECT * FROM contacts WHERE name = (?)", (to, )).fetchall() list_of_names = [] list_of_numbers = [] for i in range(0, search_results.__len__()): list_of_names.append(search_results[i][0]) if list_of_names.__contains__(to): if list_of_names.count(to) > 1: print(">> Multiple {}s were found.\n".format(to)) for some in search_results: print(some) numbers = cursor.execute( "SELECT number FROM contacts WHERE name = (?)", (to, )).fetchall() for items in numbers: for number in items: list_of_numbers.append(number) target_number = int( input( '\nEnter the number of the {} contact that you wish to text: ' .format(to))) if target_number not in list_of_numbers: print(">> Number doesn't match any of the contacts.") proceed() else: message = input("Message: ") sendSMS.send(target_number, message) print("\nYour message to {} has been sent.".format(to)) proceed() else: message = input("Message: ") number = cursor.execute( "SELECT number FROM contacts WHERE name = (?)", (to, )).fetchone()[0] sendSMS.send(number, message) print("\nYour message to {} has been sent.".format(to)) proceed() else: print(">> {} not found.".format(to)) proceed()
def change_number(name, old_number): new_number = int(input("\n>> Enter {}'s new number: ".format(name))) cursor.execute( "UPDATE contacts SET number = (?) WHERE name = (?) AND number = (?)", ( new_number, name, old_number, )) conn.commit() view_contacts()
def change_contact(): name = input('\nWhich contact would you like to change? Enter name: ') results = cursor.execute("SELECT * FROM contacts WHERE name = (?)", (name, )).fetchall() list_results = [] for items in results: for item in items: list_results.append(item) if name not in list_results: another = input('>> This contact does not exist.\n' '>> Try again? (y/n): ') if another == 'y': change_contact() elif another == 'n': proceed() else: print('>> Response not recognized.') change_contact() else: if list_results.count(name) > 1: print('\n>> There is more than one {}.'.format(name)) number_of_name = int( input( '\nEnter the number of the {} contact that you want to change: ' .format(name))) if number_of_name not in list_results: print(">> Number doesn't match. Try again.") change_contact() else: target_name = cursor.execute( "SELECT * FROM contacts WHERE number = (?)", (number_of_name, )).fetchone()[0] which_change(target_name, number_of_name) else: target_number = cursor.execute( "SELECT number FROM contacts WHERE name = (?)", (name, )).fetchone()[0] which_change(name, target_number)
def view_contacts(): contacts = pandas.DataFrame( cursor.execute("SELECT * FROM contacts ORDER BY name").fetchall()) contacts.columns = ['Name', 'Number'] print() print(contacts) proceed()
def delete_contact(): name = input('\nEnter the name of the contact you wish to delete: ') search_results = cursor.execute("SELECT * FROM contacts WHERE name = (?)", (name, )).fetchall() list_of_names = [] list_of_numbers = [] for i in range(0, search_results.__len__()): list_of_names.append(search_results[i][0]) if list_of_names.__contains__(name): if list_of_names.count(name) > 1: print(">> Multiple {}s were found.\n".format(name)) for some in search_results: print(some) numbers = cursor.execute( "SELECT number FROM contacts WHERE name = (?)", (name, )).fetchall() for items in numbers: for number in items: list_of_numbers.append(number) target_number = int( input( '\nEnter the number of the {} contact that you wish to delete: ' .format(name))) if target_number not in list_of_numbers: print(">> Number doesn't match any of the contacts.") proceed() else: cursor.execute( "DELETE FROM contacts WHERE name = (?) and number = (?)", ( name, target_number, )) conn.commit() print(">> {} has been deleted.".format(name)) view_contacts() else: cursor.execute("DELETE FROM contacts WHERE name = (?)", (name, )) conn.commit() print(">> {} has been deleted.".format(name)) view_contacts() else: print(">> {} not found.".format(name)) proceed()
def create_table(): cursor.execute("""CREATE TABLE contacts ( name text, number integer) """)