473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2391
> 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 would be on a LInux box but our domain is M$ so we...
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 an imaginary scenario: In my system (Debian...
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 usefull since a lot of PHP Scripts are coded for...
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. I am using Postgre database and to access to...
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 is expected work after upgrading OS from windows XP...
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 dont want to use the Tool ,I need the code for it...
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 to know the comments from the community. I waned...
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 the Postgre 8.0 to the Postgre 8.3. Is there...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.