sqlalchemy core: Execute Multiple Statements给出StatementError



我正在尝试使用sqlalchemy core填充一个名为dogs的表。下面是表的定义:

puppies = Table('puppies', metadata,
            Column('name', String),
            Column('date_of_birth', types.DateTime),
            Column('breed', String),
            Column('gender', String),
            Column('weight', Integer),
            Column('pic', types.Blob), ## This is the problem
            Column('shelter_id', Integer, ForeignKey('shelters.id'))
            )

下面是错误日志

Error:
    return DBAPIBinary(value)
sqlalchemy.exc.StatementError: memoryview: str object does not have the buffer interface (original cause: TypeError: memoryview: str object does not have the buffer interface) 

python: 3.4 sqlalchemy: 0.9

这个错误与字节字符串有关吗?

解决方法:类型改变。Blob到类型。NullType

为什么这个工作?

正如在SQLAlchemy IRC频道中提到的,这是因为unicode。Python3字符串是unicode字符串;如果您想使用BLOB类型,则需要使用bytes对象或支持缓冲区协议的其他对象。

SQLAlchemy还为大型unicode字符串提供了UnicodeText类型。

NullType "works"是因为SQLAlchemy直接传递它,并信任数据库驱动程序对它做正确的事情,显然在本例中它是这样做的。

最新更新