SqlAlchemy / Sqlite:InterfaceError - 是否有数据大小限制?

时间:2015-07-08 09:49:12

标签: python sqlite sqlalchemy python-3.4

我尝试使用SqlAlchemy将(一个非常大的)BLOB存储到sqlite数据库中。

对于MCVE,我使用void uart_put_c(char c) { while(!UARTTransmitterIsReady(UART1)); UARTSendDataByte(UART1, c); } 作为我要存储的BLOB。它的大小:

ubuntu-14.04.2-desktop-amd64.iso

代码

$ ls -lhubuntu-14.04.2-desktop-amd64.iso
... 996M ... ubuntu-14.04.2-desktop-amd64.iso

运行此会引发错误

from pathlib import Path
from sqlalchemy import (Column, Integer, String, BLOB, create_engine)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlite3 import dbapi2 as sqlite

SA_BASE = declarative_base()

class DbPath(SA_BASE):

    __tablename__ = 'file'

    pk_path = Column(Integer, primary_key=True)
    path = Column(String)
    data = Column(BLOB, default=None)

def create_session(db_path):

    db_url = 'sqlite+pysqlite:///{}'.format(db_path)
    engine = create_engine(db_url, module=sqlite)
    SA_BASE.metadata.create_all(engine)
    session = sessionmaker(bind=engine)

    return session()

if __name__ == '__main__':  

    pth = Path('/home/user/Downloads/iso/ubuntu-14.04.2-desktop-amd64.iso')

    with pth.open('rb') as file_pointer:
        iso_data = file_pointer.read()
    db_pth = DbPath(path=str(pth), data=iso_data)    
    db_session = create_session('test.sqlite')
    db_session.add(db_pth)
    db_session.commit()

我查看了sqlite的限制但发现没有什么可以阻止我这样做。 SqlAlchemy有限制吗?

这一切都适用于此文件:

InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported
  type. 'INSERT INTO file (path, data) VALUES (?, ?)' 
  ('/home/user/Downloads/iso/ubuntu-14.04.2-desktop-amd64.iso', <memory 
  at 0x7faf37cc18e0>)

是否有数据大小限制?或者当文件大小超过某个(哪里会是?)限制时,我该怎么办?

无论关于限制的答案是什么:我感兴趣的是如何使用SqlAlchemy将这种大小的文件存储到sqlite中?

0 个答案:

没有答案