英文:
how to acces data of <memory at 0x000...> coming from psycopg2?
问题
基本上我正在使用psycopg2,我想将几张图片保存在ByteA[]数组中。
...
但是当我尝试访问它时,它给我返回了<memory at 0x00000...>而不是图像的字节,我该如何修复这个问题??
我需要它们返回的样子就像在列表中看到的那样,这样我才能在我的网站上显示它们。
英文:
Basicaly I'm using psycopg2 and I want to save a couple of images in the ByteA[] Array
entries = psycopg2.connect("postgres://private🤫")
with entries:
with entries.cursor() as cu:
cu.execute("SELECT * FROM test")
print(cu.fetchall())
# This returns: [([<memory at 0x00000...>, <memory at 0x00000...>])]
def submit():
binary_values = (b'\x89PNG\r\n\x1a\n\x00\rIHDR\x00\x00IEND\xaeB`\x82',
b'\x89PNG\r\n\x1a\n\x00\rIHDR\x00\x00IEND\xaeB`\x82')
with entries:
with entries.cursor() as cu:
cu.execute("INSERT INTO test (photos) VALUES (ARRAY[%s, %s]);", binary_values)
But when I try to access it, it gives me <memory at 0x00000...> instead of the bytes for the images, how can I fix that??
I need them returned like they look like in the list so I can display them in my website
答案1
得分: 1
Psycopg2将BYTEA值返回为memoryview
对象。它们通常可以直接用在您原本会使用bytes
对象的地方,或者通过将它们传递给bytes(...)
构造函数来转换为bytes
对象。
英文:
Psycopg2 returns BYTEA values as memoryview
objects. They can often be used directly where you would otherwise use bytes
objects, or converted to bytes
objects by passing them to the bytes(...)
constructor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论