使用python在sqlite3中存储和检索文件

时间:2017-12-12 07:18:49

标签: python sqlite unicode-string

我在sqlite中有一个表格如下,我打算用它来存储各种文件 - 文本,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文件,存储到表中时没有错误,但检索文件时出错

  

无法解码为UTF-8列' zipfile'用文字' PK'

似乎存在一些编码或解码问题。任何指针都会有所帮助

1 个答案:

答案 0 :(得分:0)

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

Insert binary file in SQLite database with Python

最初用于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])