Esempio n. 1
0
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()
Esempio n. 2
0
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()
Esempio n. 3
0
    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()