From: Philipp Spitzer Date: Tue, 23 Sep 2025 17:31:35 +0000 (+0200) Subject: Update insert statement to work with sqlalchemy 2. X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/commitdiff_plain/d8ba29c8d3efb308ca8631970b3228bb1dd91858?hp=26256314244db4e501c6514d46ee65b88d65675c Update insert statement to work with sqlalchemy 2. --- diff --git a/owm.py b/owm.py index d97d57e..307e910 100755 --- a/owm.py +++ b/owm.py @@ -8,18 +8,18 @@ # needed packaes: python3-mysqldb python3-sqlalchemy -from pprint import pprint import argparse import configparser -import os import csv import datetime -import math import json -import warnings +import math +import os +from pprint import pprint + import sqlalchemy -from sqlalchemy import create_engine -import MySQLdb.cursors +from sqlalchemy import create_engine, Table + from seeparklib.openweathermap import openweathermap_json @@ -88,13 +88,11 @@ def write_db(config, url, weather_json, weather_data): for key, value in row.items(): if isinstance(value, float) and math.isnan(value): row[key] = None - sql_columns = list(row.keys()) - sql_values = list(row.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) + metadata = sqlalchemy.MetaData() + openweathermap_table = Table('openweathermap', metadata, autoload_with=engine) + ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row) + conn.execute(ins) + conn.commit() conn.close()