如何将二进制blob从PostgreSQL保存到文件中?

2024-05-13 19:58:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

    currSrc = connSrc.cursor()
    currSrc.execute("""SELECT request_id, time_beg, shape FROM "REQUESTS" WHERE fl_ready=1""")

shape有blob类型(bytea)。我要把它保存在FS上。如何使用Python实现它?在


Tags: 代码fromidexecutetimerequestwhererequests
1条回答
网友
1楼 · 发布于 2024-05-13 19:58:52

只需写入磁盘:

currSrc.execute("""
    SELECT request_id, time_beg, shape
    FROM "REQUESTS"
    WHERE fl_ready=1
""")
rs = cursor.fetchall()
f = open('/path/to/the/file', 'wb')
f.write(rs[0][2])
f.close()

[0][2]的原因是它是结果集的第一行和第三列。在

相关问题 更多 >