通过python在mysql中插入和检索图像

通过python在mysql中插入和检索图像,python,mysql,Python,Mysql,我使用这段代码将图像插入mysql数据库并检索回图像。 这段代码工作得很好,没有错误,但问题是即使在将图像插入img表之后,当我执行命令select*from img在mysql命令行中,它不显示任何记录 在数据库中创建的表 创建表img(图像blob不为空) 该代码在中运行良好,我发现二进制字符串(BLOB)有一个数据类型,字符串(文本)有一个数据类型。为什么要将二进制字符串(图像)转换为字符串(base64)并将其另存为二进制字符串(BLOB)?可能重复的请清除代码。例如,从PIL导入图像和

我使用这段代码将图像插入mysql数据库并检索回图像。 这段代码工作得很好,没有错误,但问题是即使在将图像插入img表之后,当我执行命令
select*from img在mysql命令行中,它不显示任何记录

在数据库中创建的表
创建表img(图像blob不为空)


该代码在中运行良好

,我发现二进制字符串(BLOB)有一个数据类型,字符串(文本)有一个数据类型。为什么要将二进制字符串(图像)转换为字符串(base64)并将其另存为二进制字符串(BLOB)?可能重复的请清除代码。例如,从PIL导入图像和导入PIL.Image。最好在回答中添加一些解释!我认为,我们可以只存储加密数据类型的文件或图像。首先,我们必须读取文件中的二进制代码。因此,将导入base64与open('.jpg','rb')一起使用,然后使用加密的base64.b64encode(照片),然后存储到mysql。如果您想使用下面的“select*from sample”代码显示来自mysql的图像,那么接下来的数据是加密的,所以我们解密base64.b64decode(数据[0][0])。然后转换成字节码。因为它来自弦。然后我们打开像文件PIL.image.open(file_-like)这样的图像,PIL表示枕头,这个模块使用将图像存储到mysql中。然后表演。非常感谢。
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()
import mysql.connector
import sys
from PIL import Image
import base64
import cStringIO
import PIL.Image

db = mysql.connector.connect(user='root', password='abhi',
                              host='localhost',
                              database='cbir')

image = Image.open('C:\Users\Abhi\Desktop\cbir-p\images.jpg')
blob_value = open('C:\Users\Abhi\Desktop\cbir-p\images.jpg', 'rb').read()
sql = 'INSERT INTO img(images) VALUES(%s)'    
args = (blob_value, )
cursor=db.cursor()
cursor.execute(sql,args)
sql1='select * from img'
db.commit()
cursor.execute(sql1)
data=cursor.fetchall()
print type(data[0][0])
file_like=cStringIO.StringIO(data[0][0])
img=PIL.Image.open(file_like)
img.show()

db.close()
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()