[英]How does python perform bitwise operations to store three values in an int?
[英]How do I perform bitwise operations on binary values in SQLalchemy?
SQLAlchemy 文档建议程序可以通过简单地使用op
function 来访问 sql 的按位运算符,例如expression.op('&')(0x04)
。 但是,当我将它用于二进制值时,会得到异常结果:
with Session(engine) as sess:
stmt = select(table.c.myvarbinary.op('&')(myBytes))
result = sess.execute(stmt).fetchone()
print(x[0])
打印b''
如果我绕过 SQLAlchemy 的 api,运算符可以正常工作:
with Session(engine) as sess:
stmt = text(f'SELECT myvarbinary & {myBytes.hex()} FROM table;')
result = sess.execute(stmt).fetchone()
print(x[0])
打印b'[expected result]'
我被阻止了这可能是什么原因。 我连接的数据库使用 MySQL。
问题是我的数据库没有将传入的字节正确解释为字节串; 我能够通过向语句中显式添加 CAST 来解决问题。
stmt = select(table.c.myvarbinary.op('&')(cast(myBytes, types.LargeBinary))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.