From: Philipp Spitzer Date: Wed, 1 Aug 2018 19:27:17 +0000 (+0200) Subject: Ignore warning about duplicate key when inserting openweathermap data. X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/commitdiff_plain/07f6fd2c6041d6203380a60771ce1cffdc2f68a7 Ignore warning about duplicate key when inserting openweathermap data. --- diff --git a/owm.py b/owm.py index c36064f..3265f20 100755 --- a/owm.py +++ b/owm.py @@ -14,7 +14,10 @@ import csv import datetime import math import json +import warnings +import sqlalchemy from sqlalchemy import create_engine +import MySQLdb.cursors from seeparklib.openweathermap import openweathermap_json @@ -85,8 +88,11 @@ def write_db(config, url, weather_json, weather_data): row[key] = None sql_columns = list(row.keys()) sql_values = list(row.values()) - sql = 'insert into openweathermap ({}) values ({})'.format(', '.join(sql_columns), ','.join(['%s'] * len(sql_columns))) - conn.execute(sql, *sql_values) + sql = 'insert ignore into openweathermap ({}) values ({})'.format(', '.join(sql_columns), ','.join(['%s'] * len(sql_columns))) + with warnings.catch_warnings(): + # ignore _mysql_exceptions.Warning: Duplicate entry '3319578-2018-08-01 20:50:00' for key 'cityid_datetime' + warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning) + conn.execute(sql, *sql_values) conn.close()