I want to allow the user to upload a picture and save it in the database(sqlite3), I tried the following, but it doesn't work:
#So, Can anyone help me, I am stuck at this?
I got this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:/Users/user/Desktop/car dealer/car dealer.py", line 505, in Add_car
self.insert_photo = self.convert_image_into_binary(photo)
NameError: name 'photo' is not defined
What I have tried:
def filedialogs(self):
global get_img
get_img = filedialog.askopenfilename(filetypes=(("png","*.png"),("jpg","*.jpg"),("Allfile","*.*")))
def convert_image_into_binary(self,filename):
with open(filname, 'rd') as file:
photo_image=file.read()
return photo_image
def Add_car(self):
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.insert_photo = self.convert_image_into_binary(photo)
sqlite_insert_blob_query = '''INSERT INTO cars_info(carmake, carmodel, caryear, cartransmition, carfuel, carcolor, carengine, carpreviousowners, carorigin, carmileage, carnumofpassengers, carlincesplatenum, carimageone) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'''
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(),self.insert_photo)
self.cursorObj.execute(sqlite_insert_blob_query, entities)
self.con.commit()
print("image and file inserted successfully as a blob into a table")
self.cursorObj.close()