#! /usr/bin/env python3

import sys
import pymysql

sys.path.insert(0, '../Mittwoch')
from utilities import hide_exception


def db_connect(credentials: dict):
    connection = pymysql.connect(**credentials)

    return connection


def list_hosts(connection):
    cursor = connection.cursor()
    # Simple: SELECT * FROM hosts
    sql = """
          SELECT name, domain, address
          FROM hosts
          ORDER BY name
          """
    cursor.execute(sql)
    # TODO: cursor.description enthaelt die Feldnamen

    # cursor.fetchone()
    # cursor.fetchall()
    # cursor ist Iterator
    for row in cursor:
        print("{0:20} {2:15} {1}".format(*row))

    # for name, domain, address in cursor:
    #     pass


def main():
    db = db_connect(credentials)

    list_hosts(db)

    # Verbindung sauber trennen
    db.close()


# Bitte aus Konfigurationsdatei lesen
credentials = dict(host='notebook14',
                   user='python',
                   password='villa',
                   database='inventory')

hide_exception(main)
