Skip to content

Instantly share code, notes, and snippets.

@iffy
Created December 21, 2011 22:50
Show Gist options
  • Save iffy/1508069 to your computer and use it in GitHub Desktop.
Save iffy/1508069 to your computer and use it in GitHub Desktop.
psycopg unicodedecodeerror
Python 2.6.2 (r262:71600, Sep 14 2009, 13:43:10)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from psycopg2 import connect
>>> from psycopg2.extras import DictCursor
>>> from psycopg2 import extensions
>>>
>>> # do what you need to get these:
... from sm.db import connstr
>>>
>>> db = connect(connstr)
>>> c = db.cursor(cursor_factory=DictCursor)
>>> c.execute('create temporary table foo (name text)')
>>> c.execute('insert into foo (name) values (%s)', ('\xc2\xa0',))
>>> c.execute('select * from foo')
>>> c.fetchone()
['\xc2\xa0']
>>>
>>> extensions.register_type(extensions.UNICODE)
>>>
>>> c.execute('select * from foo')
>>> c.fetchone()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/psycopg2/extras.py", line 61, in fetchone
res = _cursor.fetchone(self)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
>>>
>>> import psycopg2
>>> print psycopg2.__version__
2.0.14 (dt dec ext pq3)
>>>
>>> try:
... c.execute("SHOW server_version_num")
... except psycopg2.ProgrammingError:
... print 'server_version_num', 0
... else:
... print 'server_version_num', int(c.fetchone()[0])
...
server_version_num 80404
>>>
Python 2.6.2 (r262:71600, Jul 21 2010, 16:50:37)
[GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from psycopg2 import connect
>>> from psycopg2.extras import DictCursor
>>> from psycopg2 import extensions
>>>
>>> # do what you need to get these:
... from sm.db import connstr
>>>
>>> db = connect(connstr)
>>> c = db.cursor(cursor_factory=DictCursor)
>>> c.execute('create temporary table foo (name text)')
>>> c.execute('insert into foo (name) values (%s)', ('\xc2\xa0',))
>>> c.execute('select * from foo')
>>> c.fetchone()
['\xc2\xa0']
>>>
>>> extensions.register_type(extensions.UNICODE)
>>>
>>> c.execute('select * from foo')
>>> c.fetchone()
[u'\xa0']
>>>
>>> import psycopg2
>>> print psycopg2.__version__
2.0.14 (dt dec ext pq3)
>>>
>>> try:
... c.execute("SHOW server_version_num")
... except psycopg2.ProgrammingError:
... print 'server_version_num', 0
... else:
... print 'server_version_num', int(c.fetchone()[0])
...
server_version_num 80405
>>>
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from psycopg2 import connect
>>> from psycopg2.extras import DictCursor
>>> from psycopg2 import extensions
>>> connstr = 'dbname=smdtest user=smdtest host=127.0.0.1 port=5432 password='
>>> db = connect(connstr)
>>> c = db.cursor(cursor_factory=DictCursor)
>>> c.execute('create temporary table foo (name text)')
>>> c.execute('insert into foo (name) values (%s)', ('\xc2\xa0',))
>>> c.execute('select * from foo')
>>> c.fetchone()
['\xc2\xa0']
>>> extensions.register_type(extensions.UNICODE)
>>> c.execute('select * from foo')
>>> c.fetchone()
[u'\xa0']
>>> import psycopg2
>>> print psycopg2.__version__
2.2.1 (dt dec mx ext pq3)
>>> c.execute('show server_version_num')
>>> c.fetchone()
[u'80404']
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment