ProgrammingError: You must not use 8-bit bytestrings...
问题出现:
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.
产生原因:
问题在用Python的sqlite3操作数据库要插入的字符串中含有非ascii字符时产生,做插入的时候就报当前这个错误。
解决方法:
1. 按提示
connection = sqlite3.connect(...)
connection.text_factory = str
但是如果字符中出现非ascii字符,那么依然不能解决问题,会产生不可预知的乱码,这样可以参考 2
2. 以utf8的编码格式进行解码转为unicode编码做插入
cursor.execute('''
INSERT INTO JAVBUS_DATA (姓名, 年龄)
VALUES (?, ?)
''', ('张三'.decode('utf-8'), '22岁'.decode('utf-8')))
但是如果数据太长,这样一个一个敲挺麻烦的,下面是一个使用map函数简化的小例子
#-*-coding:utf-8-*-
import sqlite3
def decode_utf8(aStr):
return aStr.decode('utf-8')
conn = sqlite3.connect("something.db")
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS JAVBUS_DATA(
id INT PRIMARY KEY,
姓名 TEXT,
年龄 TEXT);''')
print "Table created successfully"
cursor.execute('''
INSERT INTO JAVBUS_DATA (姓名, 年龄)
VALUES (?, ?)
''', map(decode_utf8, ('张三', '22岁')))
cursor.close()
conn.commit()
conn.close()
其他注意:
有时用第二种方法会出现UnicodeDecodeError
加入#--coding:utf-8--
还是不行请sys指定编码:
import sys
reload(sys)
sys.setdefaultencoding('utf8')
这个问题在python3应该不会出现,python2编码问题,仅作记录
ProgrammingError: You must not use 8-bit bytestrings...的更多相关文章
- Python sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings......
完整的错误信息如下: You must not use 8-bit bytestrings unless you use a text _factory that can interpret 8-bi ...
- [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
事情是这种: 博主尝试用Python的sqlite3数据库存放加密后的usernamepassword信息,表是这种 CREATE TABLE IF NOT EXISTS user ( userID ...
- Django操作model时刻,一个错误:AttributeError:’ProgrammingError’ object has no attribute ‘__traceback__’
原因:在Django项目下对应的应用以下的models.py配置的model(也就是class)没有创建成对应的表. 这是怎么回事呢? 首先,将models.py里面的model创建成相应的数据库表的 ...
- pymysql.err.ProgrammingError: 1064 (Python字符串转义问题)
代码: sql = """INSERT INTO video_info(video_id, title) VALUES("%s","%s&q ...
- django.db.utils.ProgrammingError: 1146 的解决办法
在models中设置完数据库相关的东西后执行命令 python manage.py makemigrations 此处无错误 再次执行 python manage.py migrate 发生报错 错误 ...
- django.db.utils.ProgrammingError: (1146, "Table 'db_gold.user_ip_info' doesn't exist") RuntimeError: Model class scanhosts.models.HostLoginInfo doesn't declare an explicit app_label and isn't in an a
Error Msg 创建了一个apps的目录将所有app放入apps文件中, 将apps路径加入sys.path中:sys.insert(0, os.path.join(BASE_DIR, " ...
- sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError)
在我学习flask建立网站时间碰到了一个棘手的问题,就是在我进行操作日志的更新时间,发现表格建立有点错误,导致表缺失,从而报了下面的错误 sqlalchemy.exc.ProgrammingError ...
- pymysql.err.ProgrammingError: (1064)(字符串转译问题)
代码: sql = "insert into dm_copy(演出类型,演出场馆,剧目名称,演出地点,演出时间,演出票价,演出团体,创建时间, url)values('%s','%s','% ...
- django.db.utils.ProgrammingError: 1146 解决办法
出现原因: 因为直接在mysql中删除了表或者在执行过一次迁移后,在modles中修改了表名及对应的方法和引用 产生后果: 1.迁移的过程中可能出现表不存在的报错情况 2.迁移过程没有报错,在admi ...
随机推荐
- Spark学习之路 (十八)SparkSQL简单使用
一.SparkSQL的进化之路 1.0以前: Shark 1.1.x开始: SparkSQL(只是测试性的) SQL 1.3.x: SparkSQL(正式版本)+Dataframe 1.5.x: S ...
- ArrayList 底层实现原理
ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...
- Properties (25)
1.Properties 没有泛型.也是哈希表集合,无序集合.{a=1,b=2,c=3} 2. 读取文件中的数据,并保存到集合 (Properties方法:stringPropertyName ...
- appium随笔
目录结构如下: Test_edaike---page object设计思想 定位元素和脚本分离Images目录---用例失败截图case目录 eTestfastfood.py---定位app界面元素& ...
- 阿里巴巴 Java 代码规范
1. 抽象类命名使用 Abstratc开头. 2. 阿里强制规定不允许任何魔法值(未经定义的常量)直接出现在代码中.魔法值会让代码的可读性大大降低,而且如果同样的数值多次出现时,容易出现不清楚这些数值 ...
- Linux服务器---邮件服务openwebmail配置
配置openwebmail 通过修改配置文件openwebmail.conf,实现个性化邮箱定制.用户可以自由的更改邮箱logo.域名.容量 .签名等信息. 1.重定向index,快速打开邮箱.将下面 ...
- 018.07 New BMW ICOM A3+B+C+D Plus EVG7 Controller Tablet PC with WIFI Function
2018.07 New BMW ICOM A3+B+C+D Plus EVG7 Controller Tablet PC with WIFI Function Software Version : ...
- win10 +python3.6环境下安装opencv以及pycharm导入cv2有问题的解决办法
一.安装opencv 借鉴的这篇博客已经写得很清楚了--------https://blog.csdn.net/u011321546/article/details/79499598 ,这 ...
- 忘记MySQL root密码,如何不重启修改
说个前提:mysqld可以处理kill命令发送的信号,如SIGHUP.SIGTERM,SIGHUP信号产生的行为类似于flush命令. 不重启找回root密码首先需要有个较低权限的账号,比如可以修改t ...
- Golang获取int数组里的最大值和下标
package main import ( "fmt" ) func main() { //获取一个数组里最大值,并且拿到下标 //声明一个数组5个元素 ], , , ,} //假 ...