在 SQLAlchemy 中使用 BLOB 的示例

发布于 2024-08-11 22:28:29 字数 41 浏览 2 评论 0原文

有人有关于如何在 SQLAlchemy 中使用 BLOB 的示例吗?

Does anybody have example on how to use BLOB in SQLAlchemy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-08-18 22:28:30
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)
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)
_畞蕅 2024-08-18 22:28:30

为什么不使用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.

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

Why don't you use LargeBinary?

Extract from: 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.

I believe this might assist you.

枕梦 2024-08-18 22:28:30

从文档来看,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。

From the documentation BINARY seems the way to go: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html

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

The SQL BLOB type.

__init__(length=None) Construct a LargeBinary type.

Parameters: length – optional, a length for the column for use in DDL
statements, for those BLOB types that accept a length (i.e. MySQL). It
does not produce a lengthed BINARY/VARBINARY type - use the
BINARY/VARBINARY types specifically for those. May be safely omitted
if no CREATE TABLE will be issued. Certain databases may require a
length for use in DDL, and will raise an exception when the CREATE
TABLE DDL is issued.

ヤ经典坏疍 2024-08-18 22:28:29
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文