I got this Treeview
image table instead of image
global img, filename
self.fob = open(filename, 'rb')
self.fob = self.fob.read()
entities = (self.makecb.get(), self.modelcb.get(),self.Yearcb.get(), self.Transmissioncb.get(),self.Fuelcb.get(),self.colorcb.get(),self.Enginedisplacementcb.get(),self.PreviousownersE.get(), self.Vehicleorigincb.get(),self.mileagecb.get(), self.numofpasscb.get(),self.lincesplatenum.get().replace(" ", ""), self.fob,
self.sunroofcheck, self.leatherseatcheck,self.sensorcheck, self.cameracheck, self.AlloyWheelscheck,
self.centrallockcheck, self.monitorcheck, self.alarmsystemcheck, self.Daylightledcheck, self.Airbagcheck,
self.Seller_name.get(), self.cashepayments.get(), self.price.get(), self.lincses_start_date_E.get(),
self.linces_expiredate_E.get(), self.insurancecomp_E.get(), self.insurancecomtype_cb.get())
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.cursorObj.execute(
'''INSERT INTO Vechicle_info(carmake, carmodel, caryear, cartransmition, carfuel, carcolor, carengine, carpreviousowners, carorigin, carmileage, carnumofpassengers, carlincesplatenum, image, Sunroof, leatherseat, sensor, camera, AlloyWheels, centrallock, monitor, alarmsystem, Daylightled, Airbag, sellerrname, paymentmethod, price, Licensesdatestart, Licensedateexpire, insurancecompanyname, insurancetype) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
entities)
self.con.commit()
self.cursorObj.close()
question: I want to retrieve the blob from the sqllite3 database with other values, and insert it into treeview table using tkinter. when inserting those values into the table i got these type of binery on the on the image column \xfa\xf4\xe9\xd0\xd2\xd2\xc2\x8c\x193\xa0\xab\xab\xcb\x96Q\xcaf\ so how to fetch the image from database and then convert it to image and insert this image into treeview table tkinter.
If the image stored into database is raw data like below example:
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
# sample code to insert record to database
cursor.execute('CREATE TABLE IF NOT EXISTS sample (image BLOB, name TEXT)')
with open('sample.png', 'rb') as f:
data = f.read()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (data, 'Sample'))
cnx.commit()
cursor.close()
cnx.close()
Then you can use ImageTk.PhotoImage(data=...)
to create the image to be used in tkinter application.
Below is an example:
import sqlite3
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
root = tk.Tk()
s = ttk.Style()
s.configure('Treeview', rowheight=100) # because the image is around 100x100
tree = ttk.Treeview(root, columns=['Name'], height=2)
tree.pack()
tree.heading('#0', text='Image')
tree.heading('Name', text='Name')
cnx = sqlite3.connect('sample.db')
cursor = cnx.cursor()
cursor.execute('SELECT * FROM sample')
imglist = []
for rec in cursor:
img = ImageTk.PhotoImage(data=rec[0])
tree.insert('', 'end', image=img, values=rec[1:])
imglist.append(img)
cursor.close()
cnx.close()
root.mainloop()
And the result: