Search this site

Metadata

Articles

Projects

Presentations

python, unicode, and pysqlite2

I have filenames I'm putting into an sqlite database. These filenames are ASCII encoded and may have upper-range characters in them. The problem, in python, is that the default encoding is UTF-8 and these ASCII upper-range characters, for whatever reason, cannot be converted. pysqlite2 provides a mechanism for fixing this problem with adapters and converters.

If you get this error:

UnicodeDecodeError: 'utf8' codec can't decode bytes in position [whatever]
Here's how I fix it (this may not be the proper solution):
from pysqlite2 import dbapi2 as sqlite

# ... several lines of code later ...

def decode_string(val):
	return val

def adapt_string(val):
	return unicode(val).encode("US-ASCII")

# ... somewhere ...

sqlite.register_adapter(str, adapt_string)
sqlite.register_converter('VARCHAR', decode_string)
I'm not certain the converter/decode is necessary, however the encoding is absolutely necessary.