![]() |
[Tkinter] Insert blob into sqlite and retirve it - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Insert blob into sqlite and retirve it (/thread-30360.html) |
Insert blob into sqlite and retirve it - Maryan - Oct-17-2020 I'm struggling to find the solution for my problem. It's simple interface, button where user should select image, and the same image to be uploaded to the database. In the end the same image to be displayed into the label. Appreciate any help :) from tkinter import * from tkinter import Tk import sqlite3 from tkinter import filedialog from tkinter import messagebox root = Tk() root.title('Interface Image Insert&Retrive') root.geometry('600x600+200+200') # DB con = sqlite3.connect('example.db') cur = con.cursor() #Convert digital data to binary format def convertToBinaryData(fileimg): with open(fileimg, 'rb') as file: blobData = file.read() return blobData # Function Open def openImg(): fileimg = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("png", "*.png"))) if not fileimg: return else: try: query = cur.execute("INSERT INTO employees (employee_img) VALUES (?)") emp_Photo = convertToBinaryData(fileimg) data_tup = (emp_Photo) cur.execute((query, data_tup)) cur.commit() except sqlite3.Error as error: messagebox.showerror('Error', 'Faield to insert blob data into sqlite table') def viewImg(employee_img): try: query = "SELECT * FROM employees WHERE employee_img = ?" cur.execute(query, (employee_img,)) theimg = cur.fetchone()[0] except sqlite3.Error as error: messagebox.showerror('Error', 'Error') lbl_upload = Label(root, text = 'Please Select your image') lbl_upload.grid(row = 0, column = 0, padx = 200) btn = Button(root, text = 'Upload Image', padx = 20, command = lambda : openImg()) btn.grid(row = 1, column = 0, padx = 200) default_img = PhotoImage(file = 'default.png') display_img = Label(root, image = viewImg(), height = 250, width = 250) display_img.grid(row = 2, column = 0, padx = 200) root.mainloop() |