[Tutor] TypeError when io.open is used

Marc Tompkins marc.tompkins at gmail.com
Mon Jun 28 03:09:41 CEST 2010

On Sun, Jun 27, 2010 at 5:13 PM, <petkovas at dir.bg> wrote:

> On Sun, 27 Jun 2010 15:56:23 -0700
>  Marc Tompkins <marc.tompkins at gmail.com> wrote:
>> On Sun, Jun 27, 2010 at 3:20 PM, <petkovas at dir.bg> wrote:
>> I don't have any insight into your other piece of code, but here I think
>> you
>> just need another set of parentheses - so that the string interpolation is
>> done first, and the result of it becomes the argument to cursor.execute().
>> I can't really test it at the moment, but try changing it to:
>> cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s",
>> data1, data2) )
>> Either that, or break it into two lines:
>> myQuery = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s", data1,
>> data2
>> cursor.execute(myQuery)
>> --
>> www.fsrtechnologies.com
> Thank you for the suggestion that i should enforce the parantheses. At
> least that changed the error. Unfortunately that is wierd one, too:
> Traceback <most recent call first>:
>   File "insertdb_pg8000.py", line 20, in <module>
>      cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n =
> %s", data1, data2) )
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 314, in execute
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 319, in _execute
>   File "build\bdist.win32\egg\pg8000\interface.py", line 303, in execute
>   File "build\bdist.win32\egg\pg8000\interface.py", line 108, in _init_
>   File "build\bdist.win32\egg\pg8000\protocol.py", line 913, in _fn
>   File "build\bdist.win32\egg\pg8000\protocol.py", line 1048, in parse
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 49:
> ordinal not in range()
> I mean, as far as i know, for binary files i do not need to set encoding
> when i open them.

OK - when I answered before, I was only looking at the error message and the
actual line of code that generated it; I hadn't really looked at what you're
trying to do.

Now that I've stepped back a bit, here's what I think you're trying to
- open the file "faqns_osaka_2.jpg"
- read its contents
- write the contents to a BLOB field (or whatever the Postgres equivalent is
- anyway, a field that can hold an arbitrary chunk of data)
- write the name 'faqns_osaka_2' to a label field.

Unfortunately, you're trying to build the SQL command to do that using
string interpolation (the %s notation.)  So your program gets as far as
reading the contents of the file, and then tries to decode them into a
string  - and that's why you get this error.  Be happy you got the error -
if it had succeeded, the resulting binary data would have been unreadable as
an image, and you wouldn't have known why.

I'm certain that there are proper ways to pass chunks of binary data for
insertion into BLOB fields - people must do this every day - but I don't
actually know them.

Here's a possibility:

Or try searching for "python postgres blob".

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100627/7575211d/attachment-0001.html>

More information about the Tutor mailing list