2016-12-06 3 views
0

私のスクリプトは通常、データをMySQLデータベースに正しく挿入しますが、10秒ごとに実行するようにスケジュールします時間の約50%がクラッシュする。警告:root:値の挿入に失敗 - 必ずしもmysqlに正常に挿入されない

提案がありますか?

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") 
+1

発生したエラーを含めてください。 「MySQLdb.IntegrityError」を削除して、警告を記録するのではなく実際の例外をスローすることができます。 – joeb

+0

これが正しい出力でない場合は、コードから例外を削除しました:トレースバック(最新の最後の呼び出し): ファイル "sonic.py"、80行目 cursor.execute(sql、(distance、time )) ファイル "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py"、行174、実行中 self.errorhandler(self、exc、value) ファイル "/ usr/lib/python2 (キー "PRIMARY"のために1062、 "重複するエントリ'17 .384983 ') –

+0

あなたは試行していますが、あなたは試しています。表の規則に違反する行を挿入します。起こらないようにテーブルやコードを調整する必要があります。 – joeb

答えて

0

問題はDBのテーブルにあった、キーは私が必要と重複したエントリを、許可しなかったものを、PRIに設定しました。

PRIを削除すると問題が解決しました。ありがとう、ジョー!

関連する問題