从 Python 代码将字符串插入 SQLite 数据库时出错
当我想将 Python 代码中的字符串插入 SQLite 数据库时,出现以下错误:
sqlite3.ProgrammingError: 不得使用 8 位字节串,除非 您使用可以解释 8 位字节串的 text_factory (例如 文本工厂 = str)。强烈建议您改为 将您的应用程序切换为 Unicode 字符串。
这是插入语句:
cur.execute("insert into links (url, title, ...) values (:url, :title, ...)", locals())
字符串的产生如下:
soup = BeautifulSoup(html.read(), fromEncoding="utf-8")
html.close()
for i in soup.findAll('a'):
url = i['href']
title = i.renderContents()
你能告诉我如何将字符串插入SQLite 数据库
吗?
编辑:我发现插入到另一个表时 url
字符串没问题。 url
字符串的类型是unicode
。问题出在插入 title
字符串时。 title
字符串的类型是str
。
我尝试过:
title = unicode(i.renderContents())
但这以错误结束:
UnicodeDecodeError:“ascii”编解码器无法解码位置中的字节 0xc3 44:序号不在范围内(128)
谢谢
When I want to insert a string from Python
code to SQLite
database I get this error:
sqlite3.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.
This is the insert statement:
cur.execute("insert into links (url, title, ...) values (:url, :title, ...)", locals())
The string came into existence as follows:
soup = BeautifulSoup(html.read(), fromEncoding="utf-8")
html.close()
for i in soup.findAll('a'):
url = i['href']
title = i.renderContents()
Could you advise me how to insert the string into SQLite database
?
EDIT: I found out that url
string was OK when inserting to another table. The type of url
string was unicode
. The problem is when inserting title
string. The type of title
string is str
.
I tried:
title = unicode(i.renderContents())
but this ends with error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
44: ordinal not in range(128)
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQLite 只存储 unicode 字符串。该 URL 很可能不是 unicode,因此您需要对其进行转换。
您可以将 URL 存储为 blob(二进制),但这会使以后的生活变得更加复杂。
SQLite only stores unicode strings. It is most likely the URL that isn't unicode so you'll need to convert it.
You can store the URL as a blob (binary) instead but that will make life more complicated later.
虽然对于 url 来说这并不是绝对必要的,但您可以将其存储为 Unicode。
BeautifulSoup
使用 Unicode。在这两种情况下,url 都是
unicode
。您可以调用
isinstance()
或type()
来找出 url 的类型。您可以指定
encoding=None
来获取 Unicode:一般来说,使用
dir(obj)
、help(obj.method)
可能会有所帮助在交互式 Python 控制台中。另请参阅打印文档。Though it is not strictly necessary for an url you could store it as Unicode.
BeautifulSoup
works with Unicode.In both cases the url is
unicode
.You could call
isinstance()
ortype()
to find out what type the url has.You could specify
encoding=None
to get Unicode:In general it might be helpful to use
dir(obj)
,help(obj.method)
in a interactive Python console. See also Printing Document.