无法将 BLOB 转换为缓冲区 - Sqlite3

发布于 2024-12-05 08:23:14 字数 1699 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文