【问题标题】:Example using BLOB in SQLAlchemy在 SQLAlchemy 中使用 BLOB 的示例
【发布时间】:2010-12-19 06:41:23
【问题描述】:

有没有人举例说明如何在 SQLAlchemy 中使用 BLOB?

问候, 史蒂夫

【问题讨论】:

  • 我想看一个例子,它可以在没有 orm 的情况下工作。我猜在这种情况下,您需要以某种方式包装传递给 Connection.execute 的 blob 值。

标签: sqlalchemy blob


【解决方案1】:
from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker
import os

engine = create_engine('sqlite://', echo=True)
metadata = MetaData(engine)

sample = Table(
    'sample', metadata,
    Column('id', Integer, primary_key=True),
    Column('lob', Binary),
)

class Sample(object):

    def __init__(self, lob):
        self.lob = lob

mapper(Sample, sample)

metadata.create_all()

session = sessionmaker(engine)()

# Creating new object
blob = os.urandom(100000)
obj = Sample(lob=blob)
session.add(obj)
session.commit()
obj_id = obj.id
session.expunge_all()

# Retrieving existing object
obj = session.query(Sample).get(obj_id)
assert obj.lob==blob

【讨论】:

    【解决方案2】:
    from sqlalchemy import *
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    
    from struct import *
    
    _DeclarativeBase = declarative_base()
    
    class MyTable(_DeclarativeBase):
        __tablename__ = 'mytable'
        id = Column(Integer, Sequence('my_table_id_seq'), primary_key=True)
        my_blob = Column(BLOB)
    
    DB_NAME = 'sqlite:///C:/BlobbingTest.db'
    db = create_engine(DB_NAME)
    #self.__db.echo = True
    _DeclarativeBase.metadata.create_all(db)
    Session = sessionmaker(bind=db)
    session = Session()
    
    session.add(MyTable(my_blob=pack('H', 365)))
    l = [n + 1 for n in xrange(10)]
    session.add(MyTable(my_blob=pack('H'*len(l), *l)))
    session.commit()
    
    query = session.query(MyTable)
    for mt in query.all():
        print unpack('H'*(len(mt.my_blob)/2), mt.my_blob)
    

    【讨论】:

    • 使用包的优势。 +1
    • 考虑避免使用 import * 这样人们就不必像 pack 一样猜测/寻找对象的来源。
    • 那些打包/解包来自哪里?
    【解决方案3】:

    你为什么不用LargeBinary

    摘自:https://docs.sqlalchemy.org/en/13/core/type_basics.html#sqlalchemy.types.LargeBinary

    class sqlalchemy.types.LargeBinary(length=None)
    A type for large binary byte data.
    
    The LargeBinary type corresponds to a large and/or unlengthed binary type for the target platform, such as BLOB on MySQL and BYTEA for PostgreSQL. It also handles the necessary conversions for the DBAPI.
    

    我相信这可能会对您有所帮助。

    【讨论】:

      【解决方案4】:

      从文档 BINARY 看来是要走的路:http://docs.sqlalchemy.org/en/latest/dialects/mysql.html

      class sqlalchemy.dialects.mysql.BLOB(length=None) 基地: sqlalchemy.types.LargeBinary

      SQL BLOB 类型。

      __init__(length=None) 构造一个 LargeBinary 类型。

      参数:length - 可选,用于 DDL 的列的长度 语句,对于那些接受长度的 BLOB 类型(即 MySQL)。它 不会产生加长的 BINARY/VARBINARY 类型 - 使用 BINARY/VARBINARY 类型专门用于那些。可以安全地省略 如果不会发出 CREATE TABLE。某些数据库可能需要 在 DDL 中使用的长度,并且在 CREATE 时会引发异常 已发布 TABLE DDL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 2015-05-28
        • 2013-07-15
        • 2018-04-22
        相关资源
        最近更新 更多