使用Python循环连接SQLServer查询时,连接成功但无查询结果的问题求助
使用Python循环连接SQLServer查询时,连接成功但无查询结果的问题求助
大家好,我最近碰到一个挺头疼的问题,想请各位帮忙分析下。
先说明下我的环境:用的pymssql版本信息如下:
C:\Users\Administrator\PycharmProjects\pythonProject>pip show pymssql Name: pymssql Version: 2.2.7 Summary: DB-API interface to Microsoft SQL Server for Python. (new Cython-based version) Home-page: Author: Damien Churchill Author-email: damoxc@gmail.com
我写了一个查询SQL Server数据的函数,代码是这样的:
def QueryTaskLotSatus(taskId, lotNo): connect = pymssql.connect('192.168.5.15','sa','13234325','MyData') if connect: print(connect) print("connect success") cursor = connect.cursor() #设置变量 task_id = taskId lot_no = lotNo #query = "SELECT * FROM TaskStatus WHERE TaskId=%s AND LotNo=%s AND (Status=0 OR Status=3)" query = "SELECT TaskId, Status,StatusMessage, LotNo FROM TaskStatus WHERE TaskId=%s AND LotNo=%s" cursor.execute(query, (task_id, lot_no)) print() row = cursor.fetchone() status = 5 while row: print(row) status = row[1] # print(row[1]) # print(type(row[1])) row = cursor.fetchone() cursor.close() connect.close() return status
之后我在循环里批量调用这个函数查询数据,代码大概是这样:
for userId, taskLots in userDict.items(): waiting_count = 0 executing_count = 0 if userId not in result: result[userId] = {'userId': userId, 'waittingTaskLot': [], 'executingTaskLot': []} user_count += 1 for taskLot in taskLots: taskId, lotNo = str(taskLot).strip().split(',') try: status = QueryTaskLotSatus(taskId, lotNo) if status == 0: logging.info("success to process userId: " + str(userId) + "this user order: " + str(user_count) + "executingTaskCount: "+ str(executing_count)) result[userId]['executingTaskLot'].extend(taskLot) executing_count += 1 if status == 3: logging.info("success to process userId: " + str(userId) + "this user order: " + str(user_count) + "waitingTaskCount: " + str(waiting_count)) result[userId]['waittingTaskLot'].extend(taskLot) waiting_count += 1 except Exception as ex: write_tasklot(str(taskLot)) logging.error("call sqlserver error: " + str(ex)) continue time.sleep(1)
现在的问题是:循环执行的时候,有时候能正常从数据库拿到结果,但有时候明明打印了“connect success”,却查不到任何数据,这种情况是随机出现的,我实在摸不着头绪,有没有朋友遇到过类似的问题,或者能帮我排查下可能的问题点?
备注:内容来源于stack exchange,提问作者Boi Fox
相关产品推荐
相关产品推荐

