まず、 with open(..)
ファイルを読み取るには(別の例 )。これにより、ファイルオブジェクトが使い果たされると、または自動的に閉じられます。 例外が発生します。
# 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)
0x343234353...
を取得していると思います Management Studioで結果を確認することによるデータ:
これは、データがこのように保存されることを意味するのではなく、ManagementStudioがimage
を表す方法にすぎません。 、text
、ntext
、varbinary
結果ペインのデータ型など。