fp = open(aa, 'rb') try: img = fp.read() except: print("圖片打開出錯") fp.close()
return img
#上面的代碼讀圖片到內存
images=aa.du("222.jpg")
sql=f"insert into xsxx6 (xm,tu) values ('張三',%s);" #插入方法將圖片添加到數據表中
sql=f"update xsxx6 set xm='李四',tu =%s where id=1;"#更新方法將圖片添加到數據表中
args=(images)#形成元組
self.connect()#連接表
self.cursor.execute(sql, params)#執行插入或更新
def bu(self,aa,id):#從數據表中讀取對應ID圖片並保存為文件名為aa sql=f"select tu from xsxx6 where id={id}" fp = open(aa, 'wb') try: self.connect() self.cursor.execute(sql) self.conn.commit() fp.write(self.cursor.fetchone()[0]) self.close() except: print("圖片讀寫出錯")
上面的代碼是從數據表中讀取對應ID的圖片並保存為文件名為aa