如何使用Python从sqlite3显示BLOB对象(图像)

时间:2020-05-13 11:09:11

标签: python image sqlite blob binary-data

我在sqlite3数据库列概要文件中将图像另存为BLOB-我用相关信息召唤了函数insertBLOB:

sqliteConnection = sqlite3.connect('image_try.db')
cursor = sqliteConnection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS images (
        id INTEGER PRIMARY KEY,
        fullname TEXT,
        username TEXT,
        profile BLOB)""")

def convertToBinaryData(filename):
    with open(filename, 'rb') as file:
        blobData = file.read()
    return blobData

def insertBLOB(name, username, photo):
    sqliteConnection = sqlite3.connect('image_try.db')
    sqliteConnection.text_factory = str
    cursor = sqliteConnection.cursor()
    sqlite_insert_blob_query = """ INSERT INTO images
                              (fullname, username, profile) VALUES (?, ?, ?)"""

    empPhoto = convertToBinaryData(photo)
    data_tuple = (name, username, empPhoto)
    cursor.execute(sqlite_insert_blob_query, data_tuple)
    sqliteConnection.commit()

我试图通过调用readBlobData函数来访问图像文件(以便可以在Label中显示):

def writeTofile(data):
    # Convert binary data to proper format and write it on Hard Disk
    this = open(data, 'rb')
    this.open(io.BytesIO(base64.b64decode(data)))
    return this


def readBlobData(empId):
    try:
        sqliteConnection = sqlite3.connect('image_try.db')
        sqliteConnection.text_factory = str
        cursor = sqliteConnection.cursor()

        sql_fetch_blob_query = """SELECT * from images where id = ?"""
        cursor.execute(sql_fetch_blob_query, (empId,))
        record = cursor.fetchall()
        profile = record[0][3] #Blob object

        profile = writeTofile(profile)

        image = ImageTk.PhotoImage(profile)
        image_label = Label(root, image=image)
        image_label.photo = image
        image_label.pack()
        cursor.close()

当我调用readBlobData函数时,出现此错误:

Traceback (most recent call last):
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 90, in 
<module>
readBlobData(1)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 67, in 
readBlobData
profile = writeTofile(profile)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 51, in 
writeTofile
this = open(data, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

您知道什么是问题所在吗?以及如何解决?如何从SQLite数据库访问BLOB对象并显示它??

1 个答案:

答案 0 :(得分:0)

回溯告诉我们writeToFile函数中出现了问题,特别是当我们尝试打开文件时:

profile = writeTofile(profile)
File "C:/Users/hilab/PycharmProjects/dafyProject/addimage.py", line 51, in 
writeTofile
this = open(data, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

我们传递给函数的值是从数据库读取的二进制图像数据

profile = record[0][3]

在该函数中,我们尝试使用此二进制数据作为要从中读取文件的名称,以某种格式获取二进制数据。

def writeTofile(data):
    # Convert binary data to proper format and write it on Hard Disk
    this = open(data, 'rb')
    this.open(io.BytesIO(base64.b64decode(data)))
    return this

tkinter.PhotoImage根据文件说明需要文件的路径,因此我们必须从图像字节创建文件。

def writeTofile(data):
    # Write it to the Hard Disk
    # (ideally with a suitable name and extension)
    filename = 'myfile.img'
    with open('myfile.img', 'wb') as f:
        f.write(data)
    return filename

readBlobData中:

image = ImageTk.PhotoImage(file=profile)

然后一切都会好起来。

相关问题