saluti, sto tentando di inserire un valore bytea in una tabella di un db postgres 8.4.7<br>usando psycopg2 2.2.1-1 (Python 2.6.6) e ottengo un errore. Se qualcuno ha lumi mi puo' suggerire<br>il suo parere?<br><br>Descrivo il problema:<br>
<br># carico un'immagine <br>>>> mypic=open('/home/sdp/testpic.png','r').read()<br><br># provo a inserirla con psycopg2<br>>>> q.curs.execute("insert into blobs (name,file) values (%s,%s)",('UN nome a caso',mypic,))<br>
Traceback (most recent call last):<br> File "<stdin>", line 1, in <module><br>psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0xff<br>HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".<br>
<br>I miei tentativi di convertire a utf8 falliscono miseramente. Per esempio:<br>>>> s=unicode(mypic,'UTF-8')<br>Traceback (most recent call last):<br> File "<stdin>", line 1, in <module><br>
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode<br> return codecs.utf_8_decode(input, errors, True)<br>UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte<br>
<br><br>Provo ancora:<br>>>> s=unicode(mypic,'LATIN9')<br>>>> su8=s.encode('UTF-8')<br><br>Wow! Questo non da errore!<br>>>> len(mypic) , len(su8)<br>(13401, 19961)<br><br>lunghezze diverse, per via della diversa codifica... ok, ci puo' stare;<br>
E allora proviamo a inserire adesso:<br><br>>>> q.curs.execute("insert into blobs (name,file) values (%s,%s)",('UN nome a caso',su8))<br>>>> q.conn.commit()<br><br>Ok, no errori...<br>Proviamo a leggere il dato allora:<br>
<br>>>> q.curs.execute("select file from blobs")<br>>>> T2=q.curs.fetchone()<br>>>> T2<br>(<read-only buffer for 0xb739ad80, size 16, offset 0 at 0xb6ad04e0>,)<br>>>> dir(T2[0])<br>
['__add__', '__class__', '__cmp__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__len__', '__mul__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__']<br>
<br>quel size 16 mi inquieta, vediamo da postgres cosa dice:<br><br>=> select name,file from blobs;<br> name | file <br>----------------+------------------------------------------------------------------<br>
UN nome a caso | \303\203\302\277\303\203\302\230\303\203\302\277\303\203\302\240<br>(1 riga)<br><br>Io per ora mi arrendo; se avete idee... Grazie in anticipo<br><br>Stefano<br>