问题描述
我正在尝试创建可通过PyQt5将pdf文件上传和下载(或查看)pdf文件到我的数据库中的功能。基本上,我想实现以下步骤
import MysqL.connector
from MysqL.connector import Error
def write_file(data,filename):
# Convert binary data to proper format and write it on Hard disk
with open(filename,'wb',encoding="utf8",errors='ignore') as file:
file.write(data)
def readBLOB(emp_id,photo,bioData):
print("Reading BLOB data from python_Employee table")
try:
connection = MysqL.connector.connect(host="00.00.00.000",user="user",password="pswd",database="database")
cursor = connection.cursor()
sql_fetch_blob_query = """SELECT * from python_Employee where id = %s"""
cursor.execute(sql_fetch_blob_query,(emp_id,))
record = cursor.fetchall()
for row in record:
print("Id = ",row[0],)
print("Name = ",row[1])
image = row[2]
file = row[3]
print("Storing employee image and bio-data on disk \n")
write_file(image,photo)
write_file(file,bioData)
except MysqL.connector.Error as error:
print("Failed to read BLOB data from MysqL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MysqL connection is closed")
path1 = r'C:\Users\Bruce Ko\Desktop\Minsub Lee\Development\Git\Inventory Software\testdata.pdf'
path2 = r'C:\Users\Bruce Ko\Desktop\Minsub Lee\Development\Git\Inventory Software\eric_bioData.txt'
readBLOB(1,path1,path2)
从上述编程中,我可以从 MysqL Workbench 看到我的pdf文件已上传(即使我无法从那里读取。只有二进制信息可用,而上传的图像文件可被读取为图像)
因此,如果以上功能正确上传pdf文件,该如何下载或阅读?上面的代码来自https://pynative.com/python-mysql-blob-insert-retrieve-file-image-as-a-blob-in-mysql/,但从我上面的链接读取blob部分不起作用。它给我留下像下面这样的错误
UnicodeDecodeError:“ utf-8”编解码器无法解码位置0:无效的起始字节中的字节0xff
在此方面的任何帮助,我将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)