English 中文(简体)
Example using BLOB in SQLAlchemy
原标题:

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

问题回答
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 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)

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.

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.





相关问题
sqlalchemy does not create my foreign key

SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = as id = Column(Integer, primary_key=True) class B(Base): __tablename__ = bs id = Column(...

How to get sqlalchemy length of a string column

Consider this simple table definition (using SQLAlchemy-0.5.6) from sqlalchemy import * db = create_engine( sqlite:///tutorial.db ) db.echo = False # Try changing this to True and see what happens ...

Can SQLAlchemy update the table structure?

I am working on my first pylons + SQLAlchemy app (I m new to both). As I change my mind on the table structure, I wish there was a similar function to metadata.create_all(), that checks if there are ...

SQLAlchemy 0.5.5 - defining table schema

I used sqlautocode to generate a model.py for an existing MySQL database and here s a table example: fusion_articles = Table( fusion_articles , metadata, Column(u article_id , Integer(), ...

热门标签