所以,我尝试使用psycopg2在Python中将图像保存到PostgreSQL表中
插入查询(插入.py)在
#Folder/Picture is my path/ID is generated/IDnum is increment
picopen = open("Folder/Picture."+str(ID)+"."+str(IDnum)+".jpg", 'rb').read()
filename = ("Picture."+str(ID)+"."+str(IDnum)+".jpg")
#Sample file name is this Picture.1116578795.7.jpg
#upload_name is where I want the file name, upload_content is where I store the image
SQL = "INSERT INTO tbl_upload (upload_name, upload_content) VALUES (%s, %s)"
data = (filename, psycopg2.Binary(picopen))
cur.execute(SQL, data)
conn.commit()
现在要恢复保存的图像,我执行这个查询(恢复.py)在
^{pr2}$当我执行恢复.py它确实生成了一个“.jpg”文件,但我无法查看或打开它。在
如果有帮助的话,我会用python2.7和Centos7来做。 为了获得更多的信息,当我打开生成的文件时,我在图像查看器上得到这个。在
Error interpreting JPEG image file (Not a JPEG file: starts with 0x5c 0x78)
我也尝试过使用其他格式(.png,.bmp)
我再次检查了我的数据库类型,显然上传的内容数据类型是text它应该是bytea我以为我在创建数据库时已经将它设置为bytea。问题解决了。在
相关问题 更多 >
编程相关推荐