Postgres BYTEA problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Postgres BYTEA problems

    I've searched throughout the python website and can't find an answer.

    My problem:
    After getting a BYTEA from a postgres database using Pygresql call:
    Code:
    x = Db.query("SELECT seq FROM sequence \
    WHERE version = \'NT_004350.16\' \
    AND startloc = 1 \
    AND endloc = 1120")
    y = x.getresult()[0][1]
    'y' ends up containing a string of characters. THe problem is that
    many of these characters should be escaped characters. However,
    somewhere in the process the escape character itself is interpreted as
    a character that must be escaped.
    so if I should be getting
    "\x00\x01"
    I'll get
    "\\x00\\x01 "

    My Thoughts:
    1) Is there a way to somehow get a 'raw' uninterpreted input to the
    string?
    2) Can I possibly get the BYTEA results back as one very long Long?
    3) I can just accept that this is the way I'm going to get my results
    and try to convert the the string into something more usable.
    -Any ideas how I might go about doing this?

    Thanks for any help you can provide.

    --Steve
  • Steve

    #2
    Re: Postgres BYTEA problems

    Ivan Voras <ivoras@__geri. cc.fer.hr> wrote in message news:<c285dn$ib n$2@bagan.srce. hr>...[color=blue]
    > Steve wrote:
    >[color=green]
    > > a character that must be escaped.
    > > so if I should be getting
    > > "\x00\x01"
    > > I'll get
    > > "\\x00\\x01 "[/color]
    >
    > Are you sure that it's the dbapi driver that is doing it, or maybe the
    > data was written to the database in that way (e.g. not chr(00)+chr(01)
    > but '\' 'x' '0' '0' ...)?[/color]
    Thanks for responding Ivan

    I'm pretty sure that the table is beeing fed chr(00) + chr(01). I'm
    not the one generating the database(it's written in Java) however I've
    seen the code and it appears to be entering in the data correctly.

    Recently, to test the problem, I've gone through and inserted a string
    in python that needed escaping something like this:
    [color=blue][color=green][color=darkred]
    >>> Db.insertable(" steves",[['\001'],["test"],["\\001\\002 "]])
    >>> y = Db.query("SELEC T * FROM steves").getres ult()
    >>> print y[/color][/color][/color]
    [('\001',), ('test',), ('\\001\\002',)]

    And I got the double escape problem on both y[0] and y[2].


    Actually, looking at my testing I got an idea. Do you think this
    could have to do with the double escaping one must do while working
    with other postgres interfaces(name ly psql). Let me clarify that
    question, I'm wondering if this double escape thing is actually an
    artifact of postgres and not a problem with the pg module.

    My current solution is to do this simple loop that replaces all //###
    with the char replacemet like so:

    def no_escape(x):
    i, ret = x.find('\\'), ''
    while i != -1:
    if x[i+1].isdigit(): //number is in octal
    y = x[i+1:i+4]
    ret += x[:i] + struct.pack('B' ,string.atoi(y, 8))
    x = x[i+4:]
    i = x.find('\\')

    return ret + x

    But, of course, this solution is a little less than ideal.

    Any thoughts?

    --Steve

    Comment

    Working...