Inserting and retrieving images into mysql through python

前端 未结 2 1613
醉话见心
醉话见心 2021-01-05 16:42

I used this code to insert an image into mysql database and retrieve back the image. This code works perfectly with no errors , but the problem is even after inserting an im

2条回答
  •  逝去的感伤
    2021-01-05 17:16

    import mysql.connector
    import base64
    import io
    
    import PIL.Image
    with open('lemonyellow_logo.jpg', 'rb') as f:
        photo = f.read()
    encodestring = base64.b64encode(photo)
    db= mysql.connector.connect(user="root",password="lyncup",host="localhost",database="demo")
    mycursor=db.cursor()
    sql = "insert into sample values(%s)"
    mycursor.execute(sql,(encodestring,))
    db.commit()
    sql1="select * from sample"
    mycursor.execute(sql1)
    data = mycursor.fetchall()
    data1=base64.b64decode(data[0][0])
    file_like=io.BytesIO(data1)
    img=PIL.Image.open(file_like)
    img.show()
    db.close()
    

提交回复
热议问题