Getting binary data out of a postgre database

Discussion in 'Python' started by projecktzero, Jun 24, 2005.

  1. projecktzero

    projecktzero Guest

    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:password')
    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.
     
    projecktzero, Jun 24, 2005
    #1
    1. Advertising

  2. > 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
     
    Diez B. Roggisch, Jun 24, 2005
    #2
    1. Advertising

  3. projecktzero

    projecktzero Guest

    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
     
    projecktzero, Jun 27, 2005
    #3
  4. 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
     
    Diez B. Roggisch, Jun 27, 2005
    #4
  5. projecktzero

    projecktzero Guest

    whew!

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

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

    Thanks for the help!
     
    projecktzero, Jun 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse
    Replies:
    4
    Views:
    4,337
    Ryan Stewart
    Jun 6, 2004
  2. Replies:
    1
    Views:
    360
    Lee Harr
    May 4, 2004
  3. =?ISO-8859-1?Q?Eino_M=E4kitalo?=
    Replies:
    1
    Views:
    459
    Max M
    Dec 9, 2004
  4. Thomas

    Picking out top three out of database

    Thomas, Dec 8, 2004, in forum: ASP General
    Replies:
    9
    Views:
    151
    Bob Barrows [MVP]
    Dec 16, 2004
  5. Jagan Jacob

    Access Postgre Sql

    Jagan Jacob, Nov 29, 2010, in forum: Ruby
    Replies:
    1
    Views:
    92
    Daniel Hernández
    Nov 29, 2010
Loading...

Share This Page