3

I am working with the script below. If I change the script so I avoid the bytea datatype, I can easily copy data from my postgres table into a python variable.

But if the data is in a bytea postgres column, I encounter a strange object called memory which confuses me.

Here is the script which I run against anaconda python 3.5.2:

# bytea.py

import sqlalchemy

# I should create a conn
db_s = 'postgres://dan:[email protected]/dan'
conn = sqlalchemy.create_engine(db_s).connect()

sql_s = "drop table if exists dropme"
conn.execute(sql_s)

sql_s = "create table dropme(c1 bytea)"
conn.execute(sql_s)

sql_s = "insert into dropme(c1)values( cast('hello' AS bytea) );"
conn.execute(sql_s)

sql_s = "select c1 from dropme limit 1"
result = conn.execute(sql_s)
print(result)
# <sqlalchemy.engine.result.ResultProxy object at 0x7fcbccdade80>

for row in result:
    print(row['c1'])

# <memory at 0x7f4c125a6c48>

How to get the data which is inside of memory at 0x7f4c125a6c48 ?

1
  • try select c1::text,convert_from(c1, 'UTF8') from dropme limit 1
    – Vao Tsun
    Jan 20, 2017 at 10:20
1

You can cast it use python bytes()

for row in result:
    print(bytes(row['c1']))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.