psycopg2 insertion and reading binary data to PostgreSQL database(bytea datatype)

R

romap

Hello.
This is the full work version.

Do yuo have:
- Pyton, PostgreSQL, Psycopg2
- PostgreSQL dababase named "MyDATABASE" with table named "phonebook"
- Table "phonebook" have this columns: "lastname" [TEXT datatype] and
"c2image" [BYTEA datatype]
- Do you have an jpeg file named "sun.jpg" on c:/

Open db connection

Write image on database (binary data on bytea column)

Read image from database and write to a file
cursor = conn.cursor()
cursor.execute("SELECT (c2image) FROM phonebook WHERE lastname='MyPICTURENAME';")
mypic2 = cursor.fetchone()
open('c:/copyofsun.jpg', 'wb').write(str(mypic2[0]))

Close db connection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top