ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings
2010-10-07 23:20
1046 查看
pysqlite插入UTF-8的bytestring
2010-05-19 Python 查看评论报错ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings
修正方法,按照提示
加入
conn.text_factory = str
或者将应用切换为Unicode 字符
conn.text_factory = lambda x: unicode(x, ‘utf-8′, ‘ignore’)
By default text_factory is set to unicode(), which will use the current default encoding (ascii on my machine)
或者插入字符串前,强制转换为unicode
请查看 PySQLite的文档
http://pysqlite.googlecode.com/svn/doc/sqlite3.html#sqlite3.Connection.text_factory
http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory
请看类似的修正
http://bazaar.launchpad.net/~francesco-marella/entertainer/fix-bug-315247/revision/407?remember=407 http://issues.roundup-tracker.org/issue2277204
这个问题只在
Python 2.6/sqlite3 或者 Python 3.x中出现
我在Debian Squeeze中默认使用 Python 2.5 就没事
在 Ubuntu 10.04 中使用Python 2.6 就有这个问题
在Django中是这么修复的
if Database.version_info >= (2,4,1):
# Starting in 2.4.1, the str type is not accepted anymore, therefore,
# we convert all str objects to Unicode
# As registering a adapter for a primitive type causes a small
# slow-down, this adapter is only registered for sqlite3 versions
# needing it.
Database.register_adapter(str, lambda s:s.decode(‘utf-8′))
In Py3, anything you do “this” or use str(), you are using unicode strings.
The problem appears to be that you are passing bytestrings to sqlite; things
created with b”this” or bytes(), or read from a file as bytes and not
decoded before passing it to the database.
To really help further, you should provide the line that threw that warning
and show where any variables in it come from. As for that error message, I
believe it means text_factory = bytes.
相关文章推荐
- Python sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings......
- Python:You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byt
- Python报错:ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can
- 4000 Python报错:ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that ca
- [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
- [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
- SQLAlchemy解决ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory
- 解决python :“ProgrammingError: You must not use 8-bit bytestrings unless you...”
- mysql:If 'mysql' is not a typo you can use command-not-found to lookup the package that contains
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- Your password has expired. To log in you must change it using a client that supports expired passwor
- This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you
- Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextE
- cocoapods was not able to update the 'master' repo.if it is a unexpected issue and persists you can
- Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use
- It seems that the port 3306 is already in use.aer you sure you want to use this port
- 【DB2】If 'db2' is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found
- centos 7 The path "" is not a valid path to the 3.10.0-229.el7.x86_64 kernel headers. Would you like to change it?
- Your password has expired. To log in you must change it using a client that supports expired pass...
- Ubuntu安装VMware-tools时出现the path "" is not a valid path would you like to change it-完美解决方案