我的脚本通常会正确地将数据插入到MySQL数据库中,但是,当我将其安排为每10秒运行一次时(此调度每隔10秒调用一次此脚本),它会在大约50%的时间内崩溃。
有什么建议吗?
some unimportant code here
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * speedSound
# That was the distance there and back so halve the value
distance = distance / 2
distance = '{:f}'.format(distance)
print(distance)
time = time.strftime('%Y-%m-%d %H:%M:%S')
print(time)
# Open database connection
db = MySQLdb.connect("192.168.2.3","sonic","123456","Pi" )
cursor = db.cursor()
sql = "INSERT INTO UltraSonic (distance, time) VALUES (%s, %s)"
try:
cursor.execute(sql, (distance, time))
db.commit()
# Rollback in case there is any error
# db.rollback()
except MySQLdb.IntegrityError:
logging.warn("failed to insert values %s, %s", distance, time)
# disconnect from server
finally:
db.close()
print ("Ok")
答案 0 :(得分:0)
问题出在DB表中,Key设置为PRI,什么不允许重复输入,这是我需要的。
删除PRI解决了这个问题。谢谢joeb !!