Postgres BYTEA problems

Steve smithwis at colorado.edu
Fri Mar 12 15:29:12 EST 2004


Ivan Voras <ivoras at __geri.cc.fer.hr> wrote in message news:<c285dn$ibn$2 at bagan.srce.hr>...
> Steve wrote:
> 
> > a character that must be escaped.
> > so if I should be getting
> > "\x00\x01"
> > I'll get
> > "\\x00\\x01"
> 
> 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' ...)?
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:

   >>> Db.insertable("steves",[['\001'],["test"],["\\001\\002"]])
   >>> y = Db.query("SELECT * FROM steves").getresult()
   >>> print y
   [('\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(namely 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



More information about the Python-list mailing list