无法将 BLOB 转换为缓冲区 - Sqlite3
我正在尝试将 html 作为 blob 存储在 sqlite3 数据库中。但是,我收到以下错误“无法将 BLOB 转换为缓冲区”。我可以将 html 存储为文本,但遇到了 unicode 错误。
所以我目前的做法是这样的。
def update(self, table_name, column, value, searchColumn, searchValue, BLOB=False):
''' Update a single column with a value
column: column to update
value: Value to be updated
searchColumn: Find record with this column
searchValue: Value of search column
'''
if (BLOB == False):
sql_query = "update %s set %s = ? where %s = '%s';" % (table_name, column, value, searchColumn, searchValue)
self.conn.execute(sql_query)
else:
sql_query = "update %s set %s = ? where %s = '%s';" % (table_name, column, searchColumn, searchValue)
print sql_query
self.conn.execute(sql_query, (sqlite3.Binary(value),))
self.conn.commit()
放置 HTML 的代码是
self.urlDB.update("URL", "content", content, "address", this_url, BLOB=True)
内容 - HTML 的 Unicode 版本。由此我得到了上述错误。有人可以告诉我这段代码目前有什么问题吗? 或者,如果我可以将其另存为文本,我将如何使用上述界面将其另存为文本。 HTML 当前是这样读取的。
def fetch(self):
request, handle = self._open()
self._addHeaders(request)
if handle:
try:
data=handle.open(request)
mime_type=data.info().gettype()
url=data.geturl();
if mime_type != "text/html":
raise OpaqueDataException("Not interested in files of type %s" % mime_type,
mime_type, url)
self.content = unicode(data.read(), "utf-8",
errors="replace")
我已经看到了这个问题的其他答案,但它们似乎对这种情况没有帮助。 谢谢
I am trying to store an html as a blob in a sqlite3 DB. However, I get the following error "could not convert BLOB to Buffer". I could store the html as a TEXT but I am running into unicode errors.
So my current approach is like this.
def update(self, table_name, column, value, searchColumn, searchValue, BLOB=False):
''' Update a single column with a value
column: column to update
value: Value to be updated
searchColumn: Find record with this column
searchValue: Value of search column
'''
if (BLOB == False):
sql_query = "update %s set %s = ? where %s = '%s';" % (table_name, column, value, searchColumn, searchValue)
self.conn.execute(sql_query)
else:
sql_query = "update %s set %s = ? where %s = '%s';" % (table_name, column, searchColumn, searchValue)
print sql_query
self.conn.execute(sql_query, (sqlite3.Binary(value),))
self.conn.commit()
And the code to put the HTML is
self.urlDB.update("URL", "content", content, "address", this_url, BLOB=True)
content - Unicode version of HTML. From this I get the above error. Could someone tell me what is currently wrong with this code ?
Or if I can save it as a TEXT how would I go about using the above interface to do it to save as text. The HTML is currently read like this.
def fetch(self):
request, handle = self._open()
self._addHeaders(request)
if handle:
try:
data=handle.open(request)
mime_type=data.info().gettype()
url=data.geturl();
if mime_type != "text/html":
raise OpaqueDataException("Not interested in files of type %s" % mime_type,
mime_type, url)
self.content = unicode(data.read(), "utf-8",
errors="replace")
I have seen the other responses for this problem but they didn't seem to help in this case.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论