【问题标题】:python upload image and save it at database sqlite3python上传图像并将其保存在数据库sqlite3中
【发布时间】:2022-01-16 11:10:24
【问题描述】:

#我想让用户上传一张图片并保存在数据库中(sqlite3),我尝试了以下方法,但还是不行:

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()

#那么,谁能帮帮我,我被困在这上面了吗?

【问题讨论】:

  • 我收到了这个错误:

标签: python python-3.x database image better-sqlite3


【解决方案1】:

使用 open(filname, 'rd') 作为文件:

应该阅读

使用 open(filename, 'rb') 作为文件:

另外,当您调用self.convert_image_into_binary(photo) 时,照片 尚未定义。

【讨论】:

  • 谢谢你的回答,有什么办法可以定义(照片)以便解决问题吗?
  • 它需要设置为您要转换的图像的文件名,例如 photo = 'photo.jpg'
  • 是的,但我的目标是让用户自己上传照片,你可以看到 def filedialogs(self)
  • 那么照片应该是get_img,假设你在某处调用filedialogs。这些方法是类的一部分吗?
  • dave,感谢您的合作,感谢您的回复,我正在尝试创建允许用户上传图片然后将其保存在 sqlite3 数据库中的函数,所以首先在 def filedialogs 函数中:用户将加载图像,并在 def convert_image_into_binary: 中将图像转换为 blob,以便我可以将其添加到数据库中,但是我的方法有问题,所以有另一种方法可以帮助我实现这一点。是的,这是课程的一部分,在此先感谢
猜你喜欢
  • 2011-10-25
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2016-10-12
  • 2017-06-28
  • 1970-01-01
相关资源
最近更新 更多