Python中的MySql更新问题
我正在尝试使用for循环更新mysql表。当我运行脚本时,它只更新一行,并给出以下mysql错误 回溯(最近一次调用last):文件“index.py”,第190行,在 main()文件“index.py”,第167行,在main中 pre_cursor.execute(查询、(方框百分比、货架编号、测试编号))文件 “C:\Python27\lib\site packages\MySQLdb\cursors.py”,第205行,在 执行 errorhandler(self,exc,value)文件“C:\Python27\lib\site packages\MySQLdb\connections.py”,第36行,在 德福·阿尔特罗汉德勒 提高errorclass,errorvalue _mysql\u exceptions.InterfaceError:(0,,)Python中的MySql更新问题,python,mysql,Python,Mysql,我正在尝试使用for循环更新mysql表。当我运行脚本时,它只更新一行,并给出以下mysql错误 回溯(最近一次调用last):文件“index.py”,第190行,在 main()文件“index.py”,第167行,在main中 pre_cursor.execute(查询、(方框百分比、货架编号、测试编号))文件 “C:\Python27\lib\site packages\MySQLdb\cursors.py”,第205行,在 执行 errorhandler(self,exc,value)
任何帮助都将不胜感激。请勿关闭环路内的db连接。而且您不必在循环内执行
commit
。您正在关闭循环结束时的DB连接(不仅仅是光标)。
for ip in ip_address:
arg_list = []
action ="QuotaInfo"
arg_list.append(upnp_path)
arg_list.append(' --action=')
arg_list.append(action)
arg_list.append(' --ip=')
arg_list.append(ip)
ip_address_count = ip_address_count - 1
print "Ip adress is counting", ip_address_count
if ip_address_count == 0:
break
command = ['python', arg_list]
#Export the result of the subprocess to output variable
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = p.stdout.read()
# max_size_search = re.search(r'(quotaInfoMaxsize)\W+:\W+(\d+)', output)
# if max_size_search:
# max_size = max_size_search.group()
# max_size_digit = int(filter(str.isdigit, max_size))
# max_size_digit_gb = float(max_size_digit / 1048576)
# # print "Max size of the box is:" , max_size_digit
# used_size_search = re.search(r'(quotaInfoUsedsize)\W+:\W+(\d+)', output)
# if used_size_search:
# used_size = used_size_search.group()
# used_size_digit = int(filter(str.isdigit, used_size))
# used_size_digit_gb = float(used_size_digit / 1048576)
# # print "Used size of the box is: ", used_size_digit
# box_in_use_percentage = int(math.ceil((used_size_digit_gb * 100) / (max_size_digit_gb)))
# print "Box in use percentage:", box_in_use_percentage
# box_percentage = 100 - box_in_use_percentage
# print "box percentage", box_percentage
box_percentage = 97
test_shelf_no = re.search(r"(\d+$)+", ip)
shelf_number = int(test_shelf_no.group(1))
print "Shelf number", shelf_number
#prepare a cursor object using cursor method
pre_cursor = db.cursor()
#Query must be string, execute method does not support tuples
query = "UPDATE results_stbs SET pre_planner_percentage = %s WHERE shelf_no = %s AND results_test_id = %s"
pre_cursor.execute(query, (box_percentage, shelf_number, test_number))
db.commit()
db.close()