如何访问来自 psycopg2 的<memory at 0x000…>数据?

huangapple go评论65阅读模式
英文:

how to acces data of <memory at 0x000...> coming from psycopg2?

问题

基本上我正在使用psycopg2我想将几张图片保存在ByteA[]数组中

...

但是当我尝试访问它时它给我返回了&lt;memory at 0x00000...&gt;而不是图像的字节我该如何修复这个问题??

我需要它们返回的样子就像在列表中看到的那样这样我才能在我的网站上显示它们
英文:

Basicaly I'm using psycopg2 and I want to save a couple of images in the ByteA[] Array

entries = psycopg2.connect(&quot;postgres://private&#129323;&quot;)


with entries:
    with entries.cursor() as cu:
        cu.execute(&quot;SELECT * FROM test&quot;)
        print(cu.fetchall())
        # This returns: [([&lt;memory at 0x00000...&gt;, &lt;memory at 0x00000...&gt;])]


def submit():
    binary_values = (b&#39;\x89PNG\r\n\x1a\n\x00\rIHDR\x00\x00IEND\xaeB`\x82&#39;,
                     b&#39;\x89PNG\r\n\x1a\n\x00\rIHDR\x00\x00IEND\xaeB`\x82&#39;)
    with entries:
        with entries.cursor() as cu:
            cu.execute(&quot;INSERT INTO test (photos) VALUES (ARRAY[%s, %s]);&quot;, 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.

huangapple
  • 本文由 发表于 2023年5月25日 08:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定