added sql stuff

This commit is contained in:
2022-11-25 12:55:49 +01:00
parent aea7c693d6
commit 0c233cdce8
11 changed files with 365 additions and 0 deletions

45
Freitag/db_select.py Normal file
View File

@@ -0,0 +1,45 @@
#! /usr/bin/env python3
#
#Benötigt python3-pymysql
# mysql -h notebook14 -u python -pvilla inventory
import sys
import pymysql
from utilities import hide_exception
def db_connect(credentials: dict):
connection = pymysql.connect(**credentials)
return connection
# Passwort aus Konfigiruationsdaten lesen
credentials = dict(
host = 'notebook14',
user = 'python',
password = 'villa',
database = 'inventory'
)
def list_hosts(connection):
cursor = connection.cursor()
sql = """
SELECT name, domain, address
FROM hosts
ORDER BY name
"""
cursor.execute(sql)
#ermittlung cursor.fetchone() eine Zeile cursor.fetchmany() gibt vile in einer Zeile
# cursor ist Iterator
#test = [row for row in cursor]
for row in cursor:
print("{0:20} {2:23} {1:15}".format(*row))
#Connection
def main():
db = db_connect(credentials)
list_hosts(db)
db.close()
hide_exception(main)