import mysql.connector.locales.eng
import mysql.connector
import os
import time
os.environ['TZ'] = 'Europe/Berlin'
time.tzset()

def database(query,name,user):
    # server.start()

    mydb = mysql.connector.connect(
        host="172.17.1.21",
        port=3306,
        user="root",
        password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
        database="" + name,
        auth_plugin='mysql_native_password',
    )
    mydb.connect()
    cursor = mydb.cursor()
    if "SELECT" in query:
        if user:
            cursor.execute(query, user)
            return cursor.fetchone()
        else:
            cursor.execute(query, user)
            return cursor.fetchall()
    if "SHOW" in query:
        cursor.execute(query)
        return cursor.fetchall()
    if "INSERT" in query:
        cursor.execute(query,user)
        mydb.commit()
    if "UPDATE" in query:
        cursor.execute(query,user)
        mydb.commit()
    mydb.close()

# mysql_connect.database("SELECT * from `Ping-Server` WHERE NAME='TNPTS01' ORDER BY time DESC LIMIT 1;")
server = database("SELECT DISTINCT NAME FROM `Ping-Server`","Stines-GmbH","")
for i in server:
    print(i[0])
    time = database(f"SELECT * from `Ping-Server` WHERE NAME='{i[0]}' ORDER BY time DESC LIMIT 1;","Stines-GmbH","")
    print(time[0][0])