Insert "ignore" to SQL statement.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 4 Sep 2018 18:38:37 +0000 (20:38 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 4 Sep 2018 18:38:37 +0000 (20:38 +0200)
seewasser.py

index ecd67d6..be5f149 100755 (executable)
@@ -8,7 +8,7 @@ import sys
 import csv
 import os
 import configparser
-from sqlalchemy import create_engine, exc
+from sqlalchemy import create_engine
 import warnings
 import MySQLdb.cursors
 
@@ -77,17 +77,9 @@ def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, val
     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
     conn = engine.connect()
     with warnings.catch_warnings():
-        # ignore _mysql_exceptions.IntegrityError: (1062, "Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'")
-        # TODO: the following doesn't work
+        # ignore _mysql_exceptions.Warning: Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'
         warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning)
-        # TODO: this does but …?!
-        try:
-            conn.execute("insert into sensors (sensor_id, sensor_name, value_type, value_raw, value, timestamp) values (%s,%s,%s,%s,%s,%s)", sensor_id, sensor_name, value_type, value_raw, value, timestamp)
-        except exc.IntegrityError as e:
-            if "1062" in str(e):
-                pass
-            else:
-                raise
+        conn.execute("insert ignore into sensors (sensor_id, sensor_name, value_type, value_raw, value, timestamp) values (%s,%s,%s,%s,%s,%s)", sensor_id, sensor_name, value_type, value_raw, value, timestamp)
     conn.close()