Python &引用;can';t转义“从io.BufferedRandom到二进制”;尝试将图像插入BYTEA列时
因此,我试图通过瓶子上传一张图像,并用psycopg2将其插入postgresPython &引用;can';t转义“从io.BufferedRandom到二进制”;尝试将图像插入BYTEA列时,python,postgresql,psycopg2,bottle,Python,Postgresql,Psycopg2,Bottle,因此,我试图通过瓶子上传一张图像,并用psycopg2将其插入postgresBYTEA列,但我遇到了以下错误: TypeError: can't escape _io.BufferedRandom to binary 从将插入数据的cursor.excute()行 这是我的密码: @route('/images', method='POST') def upload_image(): upload = request.files.get('image') img = Imag
BYTEA
列,但我遇到了以下错误:
TypeError: can't escape _io.BufferedRandom to binary
从将插入数据的cursor.excute()
行
这是我的密码:
@route('/images', method='POST')
def upload_image():
upload = request.files.get('image')
img = Image.open(upload.file) # Pillow
binary = psycopg2.Binary(upload.file)
cursor = connection.cursor()
id = cursor.execute(
'''
INSERT INTO image (filename, data, width, height)
VALUES (%s, %s, %s, %s)
RETURNING id
''',
(upload.filename, binary, img.width, img.height)
)
return id
我做错了什么?我想知道问题是否在于
psycopg2.Binary
需要一个字符串,但正在接收一个类似文件的对象。你试过类似的方法吗
binary = psycopg2.Binary(upload.file.read())
注意:您可能需要首先查找文件的开头,因为(我猜)前一行的
图像。打开调用将消耗上载中的所有字节。文件很抱歉,忘记返回并接受此项,它与文件配合使用效果很好。seek(0)
:)