AFAIK SQLite returns unicode objects for TEXT in Python. Is it possible to get SQLite to return string objects instead?
3 Answers
On further inspection of the Python SQLite API, I found this little bit:
http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory
Case closed.
Comments
TEXT is intended to store text. Use BLOB if you want to store bytes.
3 Comments
c00kiemonster
Yes but why unicode necessarily? A lot of python libraries I use don't really like unicode. And I'd hate doing
str conversions because it looks ugly and sometimes I can't be sure of the type of the data that I am trying to convert.Ignacio Vazquez-Abrams
Text is Unicode. Unicode is text. Don't want Unicode? Don't use text.
Charles Duffy
@c00kiemonster, because Python 2.x
str()s don't store characters, they store bytes. Sure, it's possible to represent character strings as byte arrays, but to do that you need an encoding (and both input and output sides need to use the same one or you get ugly behavior where they don't line up); a Python 2.x str doesn't know which encoding it needs to be interpreted with to get actual characters as opposed to bytes out of it, which makes them really suboptimal for storing actual text strings.Use Python 3.2+. It will automatically return string instead of unicode (as in Python 2.7)
1 Comment
Charles Duffy
...because strings are unicode there. If the user wants a non-Unicode string, then what they would want in Python 3 is a bytestring.