SqlServer读取image类型字段并存文件

背景

金蝶云上传文件的结果会以image类型保存在SqlServer数据库,现在希望用代码的方式把这部分文件批量导出

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sql = "SELECT * FROM T_BAS_Attachment"
self.cursor.execute(sql)
res = self.cursor.fetchall()
for item in res:
content = item[10]
data = io.BytesIO(content)
filename = item[4]
if filename is not None:
directory = '/Users/insta360/Downloads/attachment/' + item[1]
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory + '/' + item[5] + '_' + item[4], 'wb') as f:
f.write(data.getvalue())
data.close()
Ciel Ni wechat
如有疑问或建议欢迎加微信讨论