469,268 Members | 1,023 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. x = Db.query("SELECT seq FROM sequence \
  2. WHERE version = \'NT_004350.16\' \
  3. AND startloc = 1 \
  4. AND endloc = 1120")
  5. y = x.getresult()[0][1]
  6.  
'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
Jul 18 '05 #1
1 2986
Ivan Voras <ivoras@__geri.cc.fer.hr> wrote in message news:<c2**********@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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by mPath Records | last post: by
reply views Thread by Ravi | last post: by
48 posts views Thread by Edwin Quijada | last post: by
1 post views Thread by Matthew Hixson | last post: by
16 posts views Thread by Michal Hlavac | last post: by
6 posts views Thread by Egyd Csaba | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.