Python: Simple CRUD Application Using SQLite - Part 2

In this tutorial we will create a Simple CRUD ( Update, Delete) Application using Python/SQLite. In my previous tutorial we already tackle the create and read on Python Simple CRUD, this time we will continue on and add some features to make the application fully functional. Before we proceed make sure you already read the previous tutorial Python: Simple CRUD Application Using SQLite - Part 1 for resources to this tutorial, because I will only add some features from the previous tutorials. So let's now do the coding. Getting started First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Installing SQLite Browser After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/. Adding an event to List Widget This code will bind the list view to trigger an event called OnSelected when the list is clicked twice. Just simple remove the previous code from the LIST WIDGET then copy the code below and paste it.
  1. scrollbary = Scrollbar(Right, orient=VERTICAL)
  2. scrollbarx = Scrollbar(Right, orient=HORIZONTAL)
  3. tree = ttk.Treeview(Right, columns=("MemberID", "Firstname", "Lastname", "Gender", "Address", "Username", "Password"), selectmode="extended", height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
  4. scrollbary.config(command=tree.yview)
  5. scrollbary.pack(side=RIGHT, fill=Y)
  6. scrollbarx.config(command=tree.xview)
  7. scrollbarx.pack(side=BOTTOM, fill=X)
  8. tree.heading('MemberID', text="MemberID", anchor=W)
  9. tree.heading('Firstname', text="Firstname", anchor=W)
  10. tree.heading('Lastname', text="Lastname", anchor=W)
  11. tree.heading('Gender', text="Gender", anchor=W)
  12. tree.heading('Address', text="Address", anchor=W)
  13. tree.heading('Username', text="Username", anchor=W)
  14. tree.heading('Password', text="Password", anchor=W)
  15. tree.column('#0', stretch=NO, minwidth=0, width=0)
  16. tree.column('#1', stretch=NO, minwidth=0, width=0)
  17. tree.column('#2', stretch=NO, minwidth=0, width=80)
  18. tree.column('#3', stretch=NO, minwidth=0, width=120)
  19. tree.column('#4', stretch=NO, minwidth=0, width=80)
  20. tree.column('#5', stretch=NO, minwidth=0, width=150)
  21. tree.column('#6', stretch=NO, minwidth=0, width=120)
  22. tree.column('#7', stretch=NO, minwidth=0, width=120)
  23. tree.pack()
  24. tree.bind('<Double-Button-1>', OnSelected)
Fixing the Button Simple add the syntax inside the Button widget, then remove the state=DISABLED from the btn_delete.
  1. btn_update = Button(Buttons, width=10, text="Update", command=Update, state=DISABLED)
  2. btn_update.pack(side=LEFT)
  3. btn_delete = Button(Buttons, width=10, text="Delete", command=Delete)
  4. btn_delete.pack(side=LEFT)
Creating the Delete Function This where the code will delete the highlighted list item from the list view when the button delete is clicked. Copy the code below and paste it inside the IDLE text editor
  1. def Delete():
  2.     if not tree.selection():
  3.        txt_result.config(text="Please select an item first", fg="red")
  4.     else:
  5.         result = tkMessageBox.askquestion('Python: Simple CRUD Applition', 'Are you sure you want to delete this record?', icon="warning")
  6.         if result == 'yes':
  7.             curItem = tree.focus()
  8.             contents =(tree.item(curItem))
  9.             selecteditem = contents['values']
  10.             tree.delete(curItem)
  11.             Database()
  12.             cursor.execute("DELETE FROM `member` WHERE `mem_id` = %d" % selecteditem[0])
  13.             conn.commit()
  14.             cursor.close()
  15.             conn.close()
  16.             txt_result.config(text="Successfully deleted the data", fg="black")
Creating the Update Function. This is where code will display the data into the Entry widget when the list item is double clicked simultaneously, then will update the data from the database when the button edit is clicked. To do that just simply copy the code below then paste it inside the IDLE text editor.
  1. def Update():
  2.     Database()
  3.     if GENDER.get() == "":
  4.         txt_result.config(text="Please select a gender", fg="red")
  5.     else:
  6.         tree.delete(*tree.get_children())
  7.         cursor.execute("UPDATE `member` SET `firstname` = ?, `lastname` = ?, `gender` =?,  `address` = ?,  `username` = ?, `password` = ? WHERE `mem_id` = ?", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), str(ADDRESS.get()), str(USERNAME.get()), str(PASSWORD.get()), int(mem_id)))
  8.         conn.commit()
  9.         cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
  10.         fetch = cursor.fetchall()
  11.         for data in fetch:
  12.             tree.insert('', 'end', values=(data[0], data[1], data[2], data[3], data[4], data[5], data[6]))
  13.         cursor.close()
  14.         conn.close()
  15.         FIRSTNAME.set("")
  16.         LASTNAME.set("")
  17.         GENDER.set("")
  18.         ADDRESS.set("")
  19.         USERNAME.set("")
  20.         PASSWORD.set("")
  21.         btn_create.config(state=NORMAL)
  22.         btn_read.config(state=NORMAL)
  23.         btn_update.config(state=DISABLED)
  24.         btn_delete.config(state=NORMAL)
  25.         txt_result.config(text="Successfully updated the data", fg="black")
  26.  
  27.  
  28. def OnSelected(event):
  29.     global mem_id;
  30.     curItem = tree.focus()
  31.     contents =(tree.item(curItem))
  32.     selecteditem = contents['values']
  33.     mem_id = selecteditem[0]
  34.     FIRSTNAME.set("")
  35.     LASTNAME.set("")
  36.     GENDER.set("")
  37.     ADDRESS.set("")
  38.     USERNAME.set("")
  39.     PASSWORD.set("")
  40.     FIRSTNAME.set(selecteditem[1])
  41.     LASTNAME.set(selecteditem[2])
  42.     ADDRESS.set(selecteditem[4])
  43.     USERNAME.set(selecteditem[5])
  44.     PASSWORD.set(selecteditem[6])
  45.     btn_create.config(state=DISABLED)
  46.     btn_read.config(state=DISABLED)
  47.     btn_update.config(state=NORMAL)
  48.     btn_delete.config(state=DISABLED)
There you have it we successfully completed the Simple CRUD Application in Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just simply visit this site. Enjoy Coding!!
Tags

Comments

Output came but functions are not working not any buttons or other functions please reply!!

Add new comment