postgres 导出bytea数据出错

这样的,做了个实验将图片存入postgres,但是发现导出数据的时候居然是内存地址。。。试了其他integer,character都是正常的。下面代码:

存入的代码,忘了说环境是python,windows

if __name__ == "__main__":      
    conn = psycopg2.connect(database="测试", 此处省略)  
    cur = conn.cursor()      
    query_sql = "select binarydump from bindump limit 1"    
    img_buffer = None
    with open("代码文件/b1.jpg",'rb') as reader: 
       img_buffer = reader.read()       
    insert_sql = "update student2 set 图片=(%s) where id=1"  
    params = (psycopg2.Binary(img_buffer),)       
    cur.execute(insert_sql, params)       
    conn.commit()  

导出的代码

conn = psycopg2.connect(database="测试", 此处省略)  
cur = conn.cursor()
cur.execute("select pic from student2 where id=1")
blob = cur.fetchone()
print(blob)
open('代码文件/post.jpg','wb').write(blob)
cur.close()

结果:
<memory at 0x000000BC50AC0108>,)
Traceback (most recent call last):
File “notebooks测试.py", line 79, in <module>
open('代码文件/post.jpg','wb').write(blob)
TypeError: a bytes-like object is required, not 'tuple'

奇了怪了。。

导入后的bytea数据是这种:
377330377340000020JFIF000001001000000001000001000000377333000C000010006006007006005010007007007011011010012014024015014013013014031022023017024035032037036035032034034 $.' ",#\034\034(7),01444\037'9=82<.342\377\300\000\013\010\000o\001\032\001\001\021\000\377\304\000\037\000\000\001\005\001\001\001\001\001\001\000\000\000\000\000\000\000\000\001\002\003\004\005\006\007\010\011\012\013\377\304\000\265\020\000\002\001\003\003\002\004\003\005\005\004\004\000\000\001}\001\002\003\000\004\021\005\022!1A\006\023Qa\007"q\0242\201\221\241\010#B\261\301\025R\321\360$3br202011012026027030031032%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz203204205206207210211212222223224225226227230231232242243244245246247250251252262263264265266267270271272302303304305306307310311312322323324325326327330331332341342343344345346347350351352361362363364365366367370371372377332000010001001000000?000367372(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242212(242271377000030370272317301:037366275375235365315250225b1772613040343072738f313000027 014347253(357\034037264W203f270212'264326`Gp255,226361225@O336;d'003257000237@k327(254277021353366>0263603753462652514202322325003?226273231211!U@365,@347003236H034327231377000303Gx?

正常么?

阅读 3.6k
1 个回答

保存的时候转为16进制字符串,取出来的时候再转换一下再保存为图片。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题