Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将BLOB转换为缓冲区-Sqlite3_Python_Sqlite_Pysqlite - Fatal编程技术网

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

Python 无法将BLOB转换为缓冲区-Sqlite3,python,sqlite,pysqlite,Python,Sqlite,Pysqlite,我正在尝试将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 t

我正在尝试将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()
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")
放置HTML的代码是

        self.urlDB.update("URL", "content", content, "address", this_url, BLOB=True)
内容-HTML的Unicode版本。由此我得到了上面的错误。有人能告诉我这个代码目前有什么问题吗? 或者,如果我可以将其另存为文本,我将如何使用上面的界面将其另存为文本。HTML当前是这样读取的

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()
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")
我已经看到了这个问题的其他答案,但在这种情况下它们似乎没有帮助。
谢谢

不要构建字符串来执行查询。在缺少的字段中使用
,并将额外的参数传递给
conn.execute
谢谢Bernado。我当然更喜欢您这样做,但我不确定如何将参数传递给函数,然后将其与conn.execute一起使用。您能给我一个小例子,说明如何将参数传递给“update”函数,并将其设置为传递给conn.execute吗?类似于(使用您的变量):
self.conn.execute('select*from?','the_table_name')
它应该是
self.conn.execute(语句、元组或_值列表)
。e、 g.
self.conn.execute(“插入%s值(?,,,,?,?);“%”曲目“,[track.id,track.title,track.artist,track.year,track.album])
,或其他任何内容。请注意,使用
不仅仅是一种方便。这是一个非常重要的安全措施,没有它,应用程序将容易受到SQL注入攻击。唯一不能用
填充的部分是标识符,如表名和字段名。因此,如果您打算从外部源接收标识符并使用它们来构建语句,那么您应该首先注意验证它们。至少要首先验证它们是标识符,例如使用
re.match(“[a-zA-Z][a-zA-Z0-9\]*”,标识符)
(超级惰性方法)。