472,342 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Getting binary data out of a postgre database

Well, I've managed to get an image into a postgre database, but now I'm
having trouble getting it out.

#! /usr/bin/env python

from pyPgSQL import PgSQL

def main():
connectdb = PgSQL.connect('server:port:database:username:passw ord')
cur = connectdb.cursor()
sqlStatement = """SELECT image from images where image_id = 1"""
cur.execute(sqlStatement)
rec = cur.fetchone()
# TODO make temp file name include image_id.
# TODO use tempfile module
# TODO clean up old temp files
tempFileName = "1.jpg"
tempFile = open(tempFileName, "w")
tempFile.write(rec[0])
tempFile.close()
cur.close()

print "Content-type: text/html\n\n"
print """"<?xml version="1.0" encoding="windows-1250"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1250" />
<title></title>
</head>

<body>
<img src="1.jpg">
</body>
</html>
"""

if __name__ == '__main__':
main()

Traceback (most recent call last):
File "./dispimage.py", line 39, in ?
main()
File "./dispimage.py", line 16, in main
tempFile.write(rec[0])
TypeError: argument 1 must be string or read-only character buffer, not
instance

So, rec[0] is an instance, but an instance of what? Since I needed to
use the PgSQL.PgBytea method on the image before inserting it into the
database, do I need to use a similar method to undo what PgBytea did to
it, or am I incorrectly writing this binary data? I tried
PgSQL.PgUnQuoteBytea(rec[0]), but that didn't work.

Can anyone show me the TypeError of my ways? Is there a good example
somewhere that shows getting binary data out of a database?

Thanks.

Jul 19 '05 #1
4 2314
> So, rec[0] is an instance, but an instance of what? Since I needed to
use the PgSQL.PgBytea method on the image before inserting it into the
database, do I need to use a similar method to undo what PgBytea did to
it, or am I incorrectly writing this binary data? I tried
PgSQL.PgUnQuoteBytea(rec[0]), but that didn't work.

What does

print rec[0]

give you?
--
Regards,

Diez B. Roggisch
Jul 19 '05 #2
Sorry for the late reply. I didn't check the group/list over the
weekend.

Anyway, I added a print rec[0] just after the fetchone. Then I ran it
from the command line, and it spewed a bunch of binary gibberish nearly
locking up Putty.

To me, it seems like it's coming out in the right format, but I'm not
sure what I'm doing wrong. If you have any ideas, I'd really
appreciate it.

Thanks,

Mike

Jul 19 '05 #3
projecktzero wrote:
Sorry for the late reply. I didn't check the group/list over the
weekend.

Anyway, I added a print rec[0] just after the fetchone. Then I ran it
from the command line, and it spewed a bunch of binary gibberish nearly
locking up Putty.

To me, it seems like it's coming out in the right format, but I'm not
sure what I'm doing wrong. If you have any ideas, I'd really
appreciate it.


What does

print rec[0].__class__

give you?

And what about using

tempFile.write(str(rec[0]))

if print really resulted in the binary data, this might help.

The DB-Api isn't too detailed about binary columns at all - the just
mentioned a Binary() object, and something about buffer objects. I'm not
sure how to ndeal with these - they seem to be mentioned in the
C-Api-Section only.
Sorry that I can't be of more help.
Diez
Jul 19 '05 #4
whew!

tempFile.write(str(rec[0])) works!

printing rec[0].__class__ puts out pyPgSQL.PgSQL.PgBytea

Thanks for the help!

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jason Tesser | last post by:
I am looking into using PostgreSQL on the backend here. I work for a Bible College. We are putting together a new software package. The database...
38
by: Kilmer C. de Souza | last post by:
Hi, I am a new user of PostgreSQL and there are some questions about its performance in a scenario with a high requisition rate Lets picture...
4
by: Brandon | last post by:
Is there a call redirector available that redirects the calls to the PHP MySql API (mysql_connect etc.) to a Postgre database? This would be very...
9
by: Leslie Coover | last post by:
http://finance.yahoo.com/ offers a wide range of financial data. Would anyone have (or know where I can get) a sample of code that
14
Sakalicek
by: Sakalicek | last post by:
Hi all, could you please help me with my big problem? I have WebService on IIS. This WebService has methods to control database stored on server. ...
1
by: nilart | last post by:
Hi, I have an installer application (Installshield) which installs postgre DB programmatically on windows: I have a requirement where postgre DB...
5
by: prajiv | last post by:
Hi, Pls guide me in synchronizing data from PostGre to Cache. i.e if we update in one database the other database must automatically get updated.I...
1
by: RRahul | last post by:
Hi, I am a database professional but have never used Postgre. My client was exploring the posiblity of using Postgre instead of Mysql and wnated...
1
by: lion cave | last post by:
hi, I'm using postgreSQL and php. I am assigned for migration of data of database. I would like to ask a help if how to migrate the data from...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.