从 Python 代码将字符串插入 SQLite 数据库时出错

发布于 2024-12-11 17:43:25 字数 946 浏览 0 评论 0原文

当我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城烟雨 2024-12-18 17:43:25

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.

梦在深巷 2024-12-18 17:43:25

虽然对于 url 来说这并不是绝对必要的,但您可以将其存储为 Unicode。

BeautifulSoup 使用 Unicode。

>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup("""<a href="ascii">""", fromEncoding="utf-8")
>>> isinstance(soup('a', href=True)[0]['href'], unicode)
True

>>> soup = BeautifulSoup("""<a href="αβγ">""", fromEncoding="utf-8")
>>> soup('a', href=True)[0]['href']
u'\u03b1\u03b2\u03b3'

在这两种情况下,url 都是 unicode

您可以调用 isinstance()type() 来找出 url 的类型。


您可以指定 encoding=None 来获取 Unicode:

i.renderContents(encoding=None)

一般来说,使用 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.

>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup("""<a href="ascii">""", fromEncoding="utf-8")
>>> isinstance(soup('a', href=True)[0]['href'], unicode)
True

>>> soup = BeautifulSoup("""<a href="αβγ">""", fromEncoding="utf-8")
>>> soup('a', href=True)[0]['href']
u'\u03b1\u03b2\u03b3'

In both cases the url is unicode.

You could call isinstance() or type() to find out what type the url has.


You could specify encoding=None to get Unicode:

i.renderContents(encoding=None)

In general it might be helpful to use dir(obj), help(obj.method) in a interactive Python console. See also Printing Document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文