python - 编程错误 : You must not use 8-bit bytestrings

标签 python python-2.7 sqlite

我试图在 python 2.7(windows) 中运行以下命令,但出现错误:

ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte strings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

#!/usr/bin/python
# -*- encoding: utf-8-*-

import sqlite3;
db = sqlite3.connect('db.sqlite3')
cursor = db.cursor()
cursor.execute('INSERT INTO X(id,a,b) VALUES(?,?,?)', (999998,"ą---ź---ć---ó---ęĘ","ssf"))
cursor.execute('SELECT * FROM X where id = 999998')
for row in cursor:
    print row

db.commit()
db.close()

如何将这些字符串(“ąźć”= 波兰符号)插入到我的 sqlite 数据库中?

最佳答案

您可以遵循他们的建议:“只需将您的应用程序切换到 Unicode 字符串即可。”

cursor.execute(
    'INSERT INTO X(id,a,b) VALUES(?,?,?)',
    (999998,u"ą---ź---ć---ó---ęĘ","ssf"))

注意字符串文字前的 u。这使它成为一个 unicode 文字。

关于python - 编程错误 : You must not use 8-bit bytestrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662004/

相关文章:

python - numpy 如何用整个 array2 替换 array1 中的单个元素

css - 如何在 GtkScrollbar 上设置最小宽度?

android - Active Android 是否容易受到 SQL 注入(inject)。任何已知的解决方案?

iphone - 将图像存储到sqlite数据库中

python - 如何使用python Regular使宽度和高度x2

python - 使用 python 中给定的 key 生成 AES 256 GCM key

python - 如何为所有处理程序设置相同的日志记录格式?

python - 使用不同的功能获得不同的对象大小

csv - 在与真实数据相同的图中绘制多项式回归

java - 设置循环播放音频文件后逐个播放