在 SQLite 中存储和检索 zip 文件给出 "Could not decode to UTF-8"

Storing and retrieving zip files in SQLite gives "Could not decode to UTF-8"

我在 SQLite 3 中有一个 table,如下所示,我计划用它来存储各种文件:txt、pdf、图像和 zip 文件。

CREATE TABLE zip (filename TEXT PRIMARYKEY NOT NULL, zipfile BLOB NOT NULL);

为了存储和检索,我正在试验以下 python 代码

#!env/bin/python


import sqlite3 as lite
import os
import sys

def insertfile(_filename):
    try:
        con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
        con.row_factory = lite.Row
        cur = con.cursor()
        cur.execute('PRAGMA foreign_keys=ON;')
        _f = open(_filename,'rb')
        _split = os.path.split(_filename)
        _file = _split[1]
        _blob = _f.read()
        cur.execute('INSERT INTO zip (filename,zipfile) VALUES (?,?)', (_file,lite.Binary(_blob)))
        _f.close()
        con.commit()
        cur.close()
        con.close()
    except Exception as ex:
        print ex

def getfile(_filename):
    try:
        con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
        con.row_factory = lite.Row
        cur = con.cursor()
        cur.execute('PRAGMA foreign_keys=ON;')
        cur.execute('SELECT zipfile from zip where filename = ?', (_filename,))
        _files = cur.fetchall()
        if len(_files) > 0:
            _file  = open('Test/'+ _filename,'wb')
            _file.write(_files[0]['zipfile'])
            _file.close()
        cur.close()
        con.close()
    except Exception as ex:
        print ex

if __name__ == '__main__':
    print 'works'
    insertfile(sys.argv[1])
    getfile(os.path.split(sys.argv[1])[1])

当我在 .txt.py.pdf 等文件上测试它时,它工作正常。

对于 Zip 文件,存储到 table 时没有错误,但在检索文件时出错:

Could not decode to UTF-8 column 'zipfile' with text 'PK '

似乎存在一些编码或解码问题。

我基本上尝试使用其中一个问题的代码

使用 Python 在 SQLite 数据库中插入二进制文件 .

它最初适用于 pdf、png、jpg 文件。但我仍然遇到 Zip 文件的错误。当我注释掉插入和 运行 检索代码时,它起作用了。现在下面的代码可以工作了。

def insertfile(_filename):
    try:
        con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
        con.row_factory = lite.Row
        cur = con.cursor()
        cur.execute('PRAGMA foreign_keys=ON;')
        _f = open(_filename,'rb')
        _split = os.path.split(_filename)
        _file = _split[1]
        _blob = _f.read()
        cur.execute('INSERT INTO zip (filename,zipfile) VALUES (?,?)', (_file,lite.Binary(_blob)))
        _f.close()
        con.commit()
        cur.close()
        con.close()
    except Exception as ex:
        print ex

def getfile(_filename):
    try:
        con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
        con.row_factory = lite.Row
        cur = con.cursor()
        cur.execute('PRAGMA foreign_keys=ON;')
        cur.execute('SELECT zipfile from zip where filename = ?', (_filename,))
        _files = cur.fetchall()
        if len(_files) > 0:
            _file  = open('Downloads/'+ _filename,'wb')
            _file.write(_files[0]['zipfile'])
            _file.close()
        cur.close()
        con.close()
    except Exception as ex:
        print ex

if __name__ == '__main__':
    print 'works'
    insertfile(sys.argv[1])
    getfile(os.path.split(sys.argv[1])[1])