经过多次尝试python3(在此语言中仍然是新的), cursor.execute 这一行将阻止for循环在条件满足时继续。但是当我评论 cursor.execute 行时,循环能够继续直到结束。我怎样才能继续直到循环的最后结果?
目标实现: - 我正在尝试从CFC_xxxx表中过滤掉一堆数据,处理并将其放回SENSOR_TREEHUGGERS表中。
显示停止发生的行: -
cursor.execute(的SQLInsert, (xxGatewayId,qqqGatewayId,treeDiameter,温度,recordTime,DATETIME treeHuggerID))
Python3代码: -
import base64
import struct
import pymysql.cursors
import sys
import datetime
from contextlib import closing
connection = pymysql.connect(host='localhost',
user='xxx',
password='xxx',
db='xxx',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with closing(connection.cursor()) as cursor:
sql = "SELECT * FROM CFC_xxxx"
cursor.execute(sql)
for row in cursor:
check = struct.unpack('>15x2f4B1L1x', base64.b64decode(row['Value']))
if check[6] > 20000 or check[6] < 10000:
continue
else:
xxGatewayId = int(row['Node_ID'])
qqqGatewayId = int(row['Gateway_ID'])
treeDiameter = int(check[0])
temperature = int(check[1])
recordTime = str(row['Timestamp'])
year = datetime.datetime.fromtimestamp(row['Timestamp']).strftime('%Y')
if check[2] == 0:
hours = '00'
else:
hours = str(check[2])
if check[3] == 0:
minute = '00'
else:
minute = str(check[3])
if check[4] < 10:
day = '0'+str(check[4])
else:
day = str(check[4])
if check[5] < 10:
month = '0'+str(check[5])
else:
month = str(check[5])
dateTime = str(year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':00')
treeHuggerID = int(check[6])
sqlInsert = "INSERT INTO SENSOR_TREEHUGGERS(`xx_Gateway_Id`,`qqq_Gateway_Id`,`treeDiameter`,`temperature`,`recordTime`,`dateTime`,`TreeHuggerId`) VALUES (%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sqlInsert, (xxGatewayId,qqqGatewayId,treeDiameter,temperature,recordTime,dateTime,treeHuggerID))
connection.commit()
答案 0 :(得分:0)
以下是我如何解决问题。使用数组附加所有已处理的数据,并使用 executemany 立即保存。在事先,必须修改mysql配置 max_allowed_packet = 500M
痛苦但有价值的教训。
答案: -
import base64
import struct
import pymysql.cursors
import sys
import datetime
from contextlib import closing
collectData = []
connection = pymysql.connect(host='localhost',
user='xxx',
password='xxx',
db='xxx',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with closing(connection.cursor()) as cursor:
sql = "SELECT * FROM CFC_xxxx"
cursor.execute(sql)
for row in cursor:
check = struct.unpack('>15x2f4B1L1x', base64.b64decode(row['Value']))
if check[6] > 20000 or check[6] < 10000:
continue
else:
xxGatewayId = int(row['Node_ID'])
qqqGatewayId = int(row['Gateway_ID'])
treeDiameter = int(check[0])
temperature = int(check[1])
recordTime = str(row['Timestamp'])
year = datetime.datetime.fromtimestamp(row['Timestamp']).strftime('%Y')
if check[2] == 0:
hours = '00'
else:
hours = str(check[2])
if check[3] == 0:
minute = '00'
else:
minute = str(check[3])
if check[4] < 10:
day = '0'+str(check[4])
else:
day = str(check[4])
if check[5] < 10:
month = '0'+str(check[5])
else:
month = str(check[5])
dateTime = str(year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':00')
treeHuggerID = int(check[6])
collectData.append(xxGatewayId,qqqGatewayId,treeDiameter,temperature,recordTime,dateTime,treeHuggerID)
c1 = connection.cursor()
sqlInsert = "INSERT INTO SENSOR_TREEHUGGERS(`xx_Gateway_Id`,`qqq_Gateway_Id`,`treeDiameter`,`temperature`,`recordTime`,`dateTime`,`TreeHuggerId`) VALUES (%s,%s,%s,%s,%s,%s,%s)"
c1.executemany(sqlInsert, collectData)
connection.commit()