netgescon-master/scripts/Script fatti per prova e per ora sospesi/conta_condomini_config.py

39 lines
942 B
Python

import json
import pymysql
import os
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(SCRIPT_DIR, "agent_config.json")
# Carica configurazione
with open(CONFIG_PATH) as f:
config = json.load(f)
MYSQL_HOST = config.get("MySQLHost", "localhost")
MYSQL_USER = config.get("MySQLUser", "user")
MYSQL_PASSWORD = config.get("MySQLPassword", "password")
MYSQL_DB = config.get("MySQLDatabase", "netgescon")
conn = pymysql.connect(
host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
db=MYSQL_DB,
charset='utf8mb4'
)
cur = conn.cursor()
cur.execute("SHOW TABLES LIKE 'condomin_%';")
tabelle = [row[0] for row in cur.fetchall()]
totale = 0
for tab in tabelle:
cur.execute(f"SELECT COUNT(*) FROM `{tab}`;")
n = cur.fetchone()[0]
print(f"{tab}: {n} condomini")
totale += n
print(f"\nTotale condomini in tutte le tabelle replicate: {totale}")
cur.close()
conn.close()