Click here to Skip to main content
15,309,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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()
Posted
Updated 12-Dec-21 20:17pm

1 solution

Read the error message, it is pretty clear:
  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
So it's line 505, in function Add_car. The line:
self.insert_photo = self.convert_image_into_binary(photo)
Is trying to use a variable called photo that ids not defined in the current function, or as a global variable in this file.

How to fix it? Probably, you meant self.insert_photo but you'd have to cjheck that was exactly what you were trying to do...
   
Comments
asaad kittaneh 13-Dec-21 16:33pm
   
thank you for your response, I am trying to create functions that allow the user to upload a picture and then save it in the sqlite3 database, so first in the def filedialogs function: the user will load an image, and in def convert_image_into_binary: it will convert the image to blob so I can add it in the database, but there is a problem with my method, so is there another method that will help me achieve this. thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900