0

Here is my code:

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

shape have type blob (bytea). I need to save it's on FS. How can I do it with Python?

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • 1
    Take a look at the link I posted, there should be no difference between PostgreSQL and MySQL blob handling. – erikvimz May 25 '16 at 12:01
  • @Erik The answer to that question was a quick fix for a problem caused by a bad design decision which was to save a binary file in a bytea column as a base64 encoded string, not as binary as it should be. – Clodoaldo Neto May 25 '16 at 13:25
  • I strongly recommend against saving files inside the database, since it leads to fast database size growth and consequently to database slowness. – caarlos0 May 25 '16 at 16:56

1 Answers1

0

Just write it to the disk:

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()

The reason for the [0][2] is that it is the first line of the result set and the third column.

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260