473,574 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ 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:dat abase:username: password')
cur = connectdb.curso r()
sqlStatement = """SELECT image from images where image_id = 1"""
cur.execute(sql Statement)
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(tempFileNa me, "w")
tempFile.write( rec[0])
tempFile.close( )
cur.close()

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

<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.PgUnQuote Bytea(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 2401
> 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.PgUnQuote Bytea(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.P gBytea

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
2084
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 use active directory. I saw that there is a PAM module for Postgre. My question is with applications that will both be standalone and web what is...
38
3112
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 Linux), there are 200.000.000 records on the database, and a total number of 10.000 diferent users. (the manual stated the following: there is a main...
4
1915
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 MySQL. I think for most PHP scripts this would work out since the SQL syntax from PostgreSQL and MySQL are very similar.
9
1767
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
4669
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 database I have Npgsql.dll library. This library is stored in GAC (global assembly cache). The WebService is not able to open the connection to the...
1
3584
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 to Vista However once I upgrade to vista somehow ./postgre windows user's password is chnaged or not working If I try to start service Postgre...
5
4064
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 2 update from Postgre or from cache.
1
3013
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 you people you post your views on the following comparision points 1] Performance 2] Scalablity 3] community support 4] Speed 5] ease of use 6]...
1
2714
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 anyone know any steps on how to migrate? Is there anyone know a script for automated migration?
0
7807
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8241
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7820
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8100
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.