Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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
Python 将pyodbc.Binary数据(BLOB)插入SQL Server映像列_Python_Sql Server 2008_Pyodbc - Fatal编程技术网

Python 将pyodbc.Binary数据(BLOB)插入SQL Server映像列

Python 将pyodbc.Binary数据(BLOB)插入SQL Server映像列,python,sql-server-2008,pyodbc,Python,Sql Server 2008,Pyodbc,我正在尝试将二进制数据插入SQL Server数据库中数据类型为image的列中。我知道varbinary(max)是首选的数据类型,但我无权更改模式 无论如何,我正在读取一个文件的内容,并将其包装在pyodbc.Binary()中,如下所示: f = open('Test.ics', 'rb') ablob = f.read().encode('hex') ablob = pyodbc.Binary(ablob) 当我打印repr(ablob)时,我看到了正确的值bytearray(b'42

我正在尝试将二进制数据插入SQL Server数据库中数据类型为
image
的列中。我知道
varbinary(max)
是首选的数据类型,但我无权更改模式

无论如何,我正在读取一个文件的内容,并将其包装在pyodbc.Binary()中,如下所示:

f = open('Test.ics', 'rb')
ablob = f.read().encode('hex')
ablob = pyodbc.Binary(ablob)
当我打印repr(ablob)时,我看到了正确的值
bytearray(b'424547494e3a5…
(添加了省略号)

然而,在插入

insertSQL = """insert into documents(name, documentType, document, customerNumber) values(?,?,?,?)"""
cur.execute(insertSQL, 'test200.ics', 'text/calendar', pyodbc.Binary(ablob), 1717)
文档列的值为
0x343234353…
,看起来好像十六进制数据已转换为ASCII字符代码

我以为用pyodbc.Binary()包装这个值就可以解决这个问题了?任何帮助都将不胜感激

我使用的是Python2.7和SQLServer2008R2(10.50)

编辑

beargle善意地指出,我没有必要调用encode('hex'),这导致了我的问题。我相信这一定是将数据强制转换为字符串(尽管更全面的解释会有所帮助)

工作代码:

ablob = pyodbc.Binary(f.read())
cur.execute(insertSQL, 'test200.ics', 'text/calendar', ablob, 1717)
首先确保您使用读取文件()。当文件对象耗尽或引发异常时,这会自动关闭文件对象

# common vars
connection = pyodbc.connect(...)
filename = 'Test.ics'
insert = 'insert into documents (name, documentType, document, customerNumber)'

# without hex encode
with open(filename, 'rb'):
    bindata = f.read()

# with hex encode
with open(filename, 'rb'):
    hexdata = f.read().encode('hex')

# build parameters
binparams = ('test200.ics', 'text/calendar', pyodbc.Binary(bindata), 1717)
hexparams = ('test200.ics', 'text/calendar', pyodbc.Binary(hexdata), 1717)

# insert binary
connection.cursor().execute(insert, binparams)
connection.commit()

# insert hex
connection.cursor().execute(insert, hexparams)
connection.commit()

# print documents
rows = connection.cursor().execute('select * from documents').fetchall()
for row in rows:
    try:
        # this will decode hex data we inserted
        print str(row.document).decode('hex')
    # attempting to hex decode binary data throws TypeError
    except TypeError:
        print str(row.document)
通过查看Management Studio中的结果,我猜您正在获得
0x343234353…
数据:


这并不意味着数据是以这种方式存储的,它只是Management Studio在结果窗格中表示
图像
文本
ntext
varbinary
,等等数据类型的方式。

我使用的是
SQL Server原生客户端10.0
驱动程序,还使用了
SQL Server
驱动程序进行了测试同样的结果。为什么在使用
pyodbc.Binary
包装之前需要对内容进行十六进制编码?很好,我不需要这样做,删除导联可以得到预期的结果。谢谢!很高兴听到。我确实使用十六进制编码得到了这个结果,您只需对选择的结果进行解码。在我的情况下,还有另一个应用程序可以读取数据因此,我无法控制二进制数据的解码。由于第2行中的值,另一个应用程序最终下载了一个损坏的文件。