added sql stuff
This commit is contained in:
45
Freitag/db_select.py
Normal file
45
Freitag/db_select.py
Normal 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)
|
||||
Reference in New Issue
Block a user