3

AFAIK SQLite returns unicode objects for TEXT in Python. Is it possible to get SQLite to return string objects instead?

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

4

TEXT is intended to store text. Use BLOB if you want to store bytes.

3 Comments

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.
Text is Unicode. Unicode is text. Don't want Unicode? Don't use text.
@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.
0

Use Python 3.2+. It will automatically return string instead of unicode (as in Python 2.7)

1 Comment

...because strings are unicode there. If the user wants a non-Unicode string, then what they would want in Python 3 is a bytestring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.