Python Forum
Button to add data to database and listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button to add data to database and listbox
#1
Hello. I am creating a database that holds info on a person's name and phone number. The names are displayed in a listbox and when the user clicked a name a message box appears that displays their info.

There are also 2 entry boxes, one to input a new name and one to input a new phone number. Then the user clicks the 'add' button and it should update the listbox display and database. But I can't get my 'add' button to work. When I run the program I get the error:
Error:
FOREIGN KEY constraint failed
I don't understand why I get this error. Can you please help me? Here is my code:

    def create_db(self):
        conn = None
        try:
            conn = sqlite3.connect('phonebook.db')
            cur = conn.cursor()

            cur.execute('PRAGMA foreign_keys=ON')

            # add phone table
            cur.execute('DROP TABLE IF EXISTS Phones')
            cur.execute('''CREATE TABLE Phones (PhoneID INTEGER PRIMARY KEY NOT NULL,
                PhoneNumber TEXT)''')

            cur.execute('DROP TABLE IF EXISTS Names')
            cur.execute('''CREATE TABLE Names (NameID INTEGER PRIMARY KEY NOT NULL,
                Name TEXT,
                PhoneID TEXT,
                FOREIGN KEY(PhoneID) REFERENCES Phones(PhoneID))''')

            # add rows to Phones table
            cur.execute('''INSERT INTO Phones(PhoneNumber)
                VALUES ("555-1212"),
                ("555-0101"),
                ("555-9090"),
                ("333-1234"),
                ("555-2345")''')

            # add row to Names table
            cur.execute('''INSERT INTO Names (Name, PhoneID)
                VALUES ("Jason Lee", 1),
                ("Amanda Green", 2),
                ("Jenna Jacobs", 3),
                ("Alfredo Greer", 4),
                ("Jules Landis", 5)''')

            conn.commit()
        except sqlite3.Error as err:
            print(err)
        finally:
            if conn != None:
                conn.close()
# create entry field
    def __build_entries(self):
        self.name_entry = tkinter.Entry(self.main_window,
                                        width=15)
        self.phone_entry = tkinter.Entry(self.main_window,
                                         width=10)

        name = self.name_entry.get()
        phone = self.phone_entry.get()
        conn = None
        try:
            conn = sqlite3.connect('phonebook.db')
            cur = conn.cursor()
            cur.execute('''INSERT INTO Names (Name, PhoneID)
                        VALUES (?, ?)''',
                        (name, phone))

            conn.commit()
        except sqlite3.Error as err:
            print(err)
        finally:
            if conn != None:
                conn.close()
        self.add_button = tkinter.Button(self.main_window,
                                         text='add',
                                         command=self.__populate_listbox())

        self.name_entry.pack(side='left')
        self.phone_entry.pack(side='left')
        self.add_button.pack(side='left')
# get list from database
    def __get_phones(self):
        name_list = []
        conn = None
        try:
            conn = sqlite3.connect('phonebook.db')
            cur = conn.cursor()

            cur.execute('SELECT Name from Names')
            name_list = [n[0] for n in cur.fetchall()]

        except sqlite3.Error as err:
            tkinter.messagebox.showinfo('Database error:', err)
        finally:
            if conn != None:
                conn.close()
        return name_list

    # fill listbox with database list
    def __populate_listbox(self):
        for name in self.__get_phones():
            emp_list = self.phone_listbox.get(0, tkinter.END)
            if name not in emp_list:
                self.phone_listbox.insert(tkinter.END, name)
Reply
#2
Please show complete, unaltered error traceback.
The snippet you show could apply to any of the SQL statements in your code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Error verify data in database TomasSanchexx 2 861 Aug-11-2023, 12:37 PM
Last Post: TomasSanchexx
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 4,927 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  [Tkinter] Trying to add data into a shelf from a submit button TWB 8 1,809 Jan-06-2023, 11:30 PM
Last Post: TWB
  Can't get tkinter button to change color based on changes in data dford 4 3,363 Feb-13-2022, 01:57 PM
Last Post: dford
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 7,313 Dec-15-2021, 08:48 PM
Last Post: Hilal
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,024 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 6,873 Jan-02-2020, 04:32 PM
Last Post: Denni
  [Tkinter] Listbox and Button not showing Linuxdesire 2 3,791 Oct-05-2019, 06:33 PM
Last Post: woooee
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Listbox data samtal 5 6,664 Oct-18-2018, 03:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020