Skip to main content
 首页 » 编程设计

Python:从 MSSQL 检索图像

2025年05月04日9lexus

我正在开发一个从 MSSQL 检索图像的 Python 项目。我的代码能够成功检索图像,但大小固定为 63KB。如果图像大于该大小,它只会从图像中提取前 63KB!

以下是我的代码:

#!/usr/bin/python 
import _mssql 
 
mssql=_mssql.connect('<ServerIP>','<UserID>','<Password>') 
mssql.select_db('<Database>') 
 
x=1 
while x==1: 
    query="select TOP 1 * from table;" 
    if mssql.query(query): 
        rows=mssql.fetch_array() 
        rowNumbers = rows[0][1] 
        #print "Number of rows fetched: " + str(rowNumbers) 
        for row in rows: 
        for i in range(rowNumbers): 
            FILE=open('/home/images/' + str(row[2][i][1]) + '-' + str(row[2][i][2]).strip() + ' (' + str(row[2][i][0]) + ').jpg','wb') 
            FILE.write(row[2][i][4]) 
            FILE.close() 
            print 'Successfully downloaded image: ' + str(row[2][i][0]) + '\t' + str(row[2][i][2]).strip() + '\t' + str(row[2][i][1]) 
    else: 
        print mssql.errmsg() 
        print mssql.stdmsg() 
mssql.close() 

请您参考如下方法:

当您使用这样的数据库时,很难判断问题出在哪里。您的查询没有明确选择任何列,因此我们不知道您的表结构是什么,也不知道这些列是什么类型。我怀疑表格格式不是您所期望的,或者您的数据的列类型不正确。

此外,您的代码甚至看起来都无法运行。你有“for row in rows:”,然后不要缩进。也许发布您的架构?