On Sun, Jun 27, 2010 at 5:13 PM, <span dir="ltr"><<a href="mailto:petkovas@dir.bg">petkovas@dir.bg</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Sun, 27 Jun 2010 15:56:23 -0700<br>
Marc Tompkins <<a href="mailto:marc.tompkins@gmail.com" target="_blank">marc.tompkins@gmail.com</a>> wrote:<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
On Sun, Jun 27, 2010 at 3:20 PM, <<a href="mailto:petkovas@dir.bg" target="_blank">petkovas@dir.bg</a>> wrote:<br>
<br></div><div class="im">
I don't have any insight into your other piece of code, but here I think you<br>
just need another set of parentheses - so that the string interpolation is<br>
done first, and the result of it becomes the argument to cursor.execute().<br>
I can't really test it at the moment, but try changing it to:<br>
cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s",<br>
data1, data2) )<br>
<br>
Either that, or break it into two lines:<br>
<br>
myQuery = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s", data1,<br>
data2<br>
cursor.execute(myQuery)<br>
<br>
-- <br>
<a href="http://www.fsrtechnologies.com" target="_blank">www.fsrtechnologies.com</a><br>
</div></blockquote>
<br>
Thank you for the suggestion that i should enforce the parantheses. At least that changed the error. Unfortunately that is wierd one, too:<br>
<br>
Traceback <most recent call first>:<br>
File "insertdb_pg8000.py", line 20, in <module><div class="im"><br>
cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s", data1, data2) )<br>
File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn<br></div>
File "build\bdist.win32\egg\pg8000\dbapi.py", line 314, in execute<br>
File "build\bdist.win32\egg\pg8000\dbapi.py", line 319, in _execute<br>
File "build\bdist.win32\egg\pg8000\interface.py", line 303, in execute<br>
File "build\bdist.win32\egg\pg8000\interface.py", line 108, in _init_<br>
File "build\bdist.win32\egg\pg8000\protocol.py", line 913, in _fn<br>
File "build\bdist.win32\egg\pg8000\protocol.py", line 1048, in parse<br>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 49: ordinal not in range()<br>
<br>
I mean, as far as i know, for binary files i do not need to set encoding when i open them.<br></blockquote><div><br>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.<br>
<br>Now that I've stepped back a bit, here's what I think you're trying to achieve:<br>- open the file "faqns_osaka_2.jpg"<br>- read its contents<br>- write the contents to a BLOB field (or whatever the Postgres equivalent is - anyway, a field that can hold an arbitrary chunk of data)<br>
- write the name 'faqns_osaka_2' to a label field.<br><br>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.<br>
<br>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. <br><br>Here's a possibility:<br><a href="http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-7-sect-11.html">http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-7-sect-11.html</a><br>
<br>Or try searching for "python postgres blob".<br></div></div><br><br clear="all"><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>