Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用sqlalchemy将值插入VARBINARY列_Sql Server_Sqlalchemy - Fatal编程技术网

Sql server 使用sqlalchemy将值插入VARBINARY列

Sql server 使用sqlalchemy将值插入VARBINARY列,sql-server,sqlalchemy,Sql Server,Sqlalchemy,我正在使用MS SQL数据库,以及Python和sqlalchemy。一个表的列定义为 CREATE TABLE MyTable ( ... [address] [varbinary] (16) NULL ... 表类MyTable是通过表反射生成的。 在Python代码中为MyTable.address中的MyTable.address指定一个字符串值会触发异常 sqlalchemy.exc.StatementError: (builtins.TypeError) s

我正在使用MS SQL数据库,以及Python和sqlalchemy。一个表的列定义为

CREATE TABLE MyTable (
    ...
    [address] [varbinary] (16) NULL
    ...
表类
MyTable
是通过表反射生成的。 在Python代码中为MyTable.address中的
MyTable.address指定一个字符串值会触发异常

sqlalchemy.exc.StatementError: (builtins.TypeError) string argument without an encoding
将其写入数据库时。相反,通过数组传递
会产生数据库错误

(pymssql.OperationalError) (257, 
b'Implicit conversion from data type varchar to varbinary is not allowed. 
Use the CONVERT function to run this query.DB-Lib error message 20018, severity 16 [...]
我知道在二进制列中存储字符串需要编码,但如何提供这种编码呢?我还可以如何使用sqlalchemy的ORM功能将二进制数据写入数据库

因为我是通过反射创建表类的,所以不能像下面的回答中所述那样直接修改SQL代码


读取也不会产生任何解决方案。

在将数据保存到数据库之前,尝试先将数据转换为varbinary,因为地址字段的数据类型是varbinary

select convert(varbinary,[the string or data to be convert])

假设像我的例子一样,
address
是一个大整数,我们可以写

a = 72037797995605455
MyTable.address = a.to_bytes(16, 'big')

似乎我没有明确表示我想使用sqlalchemy的ORM来避免编写SQL代码。我相应地改变了我的问题。