Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
9e78b44184 | |||
7f41480cc8 | |||
67c7d20b1b | |||
3598abe51f | |||
a2369f3863 |
@@ -21,4 +21,5 @@ CC-BY-SA Olli Graf
|
||||
|14 | Logging|
|
||||
|15 | GPIO|
|
||||
|16 | Testing|
|
||||
|17 | Datenbank|
|
||||
|
||||
|
1
teil15/.gitignore
vendored
1
teil15/.gitignore
vendored
@@ -1,3 +1,2 @@
|
||||
./__pycache__
|
||||
*.log
|
||||
./network/__pycache__/*
|
||||
|
Binary file not shown.
Binary file not shown.
0
testing/.gitignore → teil16/.gitignore
vendored
0
testing/.gitignore → teil16/.gitignore
vendored
@@ -52,8 +52,6 @@ class TestFib(unittest.TestCase):
|
||||
if isinstance(key, numbers.Number): #Überspringen der Überschriftenzeile
|
||||
self.assertEqual(result, fib(int(key)))
|
||||
|
||||
print(f'Testdatentyp: ${type(data)}')
|
||||
print(f'Testdaten ${results}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
3
teil17/.gitignore
vendored
Normal file
3
teil17/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
./__pycache__
|
||||
./db.ini
|
||||
./network/__pycache__/*
|
45
teil17/DBConnection.py
Normal file
45
teil17/DBConnection.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#encoding: utf-8
|
||||
import mysql.connector
|
||||
import logging
|
||||
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
class DBConnection():
|
||||
|
||||
def __init__(self):
|
||||
self.dbconnection = None
|
||||
|
||||
#Verbindung zur Datenbank mit den übergebenen Wreten herstellen.
|
||||
def connect(self, host, user, password, database):
|
||||
|
||||
self.connection= mysql.connector.connect(host=host, user=user,password=password,database=database)
|
||||
|
||||
# Verbindung trennen
|
||||
def disconnect(self):
|
||||
if self.connection is not None:
|
||||
self.connection.close()
|
||||
self.connection = None
|
||||
|
||||
# Schüler-Record in die Tabelle einfügen.
|
||||
def insertSchueler(self,schueler):
|
||||
self.execute_query('insert into schueler (name,vorname) values(%s,%s)', (schueler['name'],schueler['vorname']))
|
||||
|
||||
# Liefert True, wenn die Verbindung zur DB besteht, sonst False
|
||||
def isConnected(self):
|
||||
if self.connection is not None:
|
||||
return self.connection.is_connected()
|
||||
return False
|
||||
|
||||
# Wenn DB-Verbindung besteht, wird der Cursor geliefert, sonst None
|
||||
def cursor(self):
|
||||
if self.isConnected():
|
||||
return self.connection.cursor()
|
||||
return None
|
||||
|
||||
# Datenbank Kommando ausführen. Das params-Tupel wird in die Query eingesetzt.
|
||||
# Liefert ein Resultset zurück.
|
||||
def execute_query(self, query, params = None):
|
||||
if self.isConnected():
|
||||
cursor = self.cursor()
|
||||
if cursor is not None:
|
||||
cursor.execute(query,params) #Kommdo ausführen
|
||||
return cursor.fetchall() # Ergebnis zurückliefern.
|
BIN
teil17/__pycache__/DBConnection.cpython-39.pyc
Normal file
BIN
teil17/__pycache__/DBConnection.cpython-39.pyc
Normal file
Binary file not shown.
9
teil17/connectdb.py
Normal file
9
teil17/connectdb.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from DBConnection import DBConnection
|
||||
import logging
|
||||
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
db_connection = DBConnection()
|
||||
|
||||
db_connection.connect('database','piuser','SW_Ep.6:ROTJ','pidb')
|
||||
logging.debug(f'Datenbankverbindung aufgebaut ${db_connection.isConnected()}')
|
||||
|
8
teil17/db.ini.sample
Normal file
8
teil17/db.ini.sample
Normal file
@@ -0,0 +1,8 @@
|
||||
[pidb]
|
||||
host=database
|
||||
user=piuser
|
||||
password=<passwort>
|
||||
database=pidb
|
||||
|
||||
|
||||
|
88
teil17/testdb.py
Executable file
88
teil17/testdb.py
Executable file
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/python3
|
||||
# encoding:utf-8
|
||||
|
||||
import unittest
|
||||
from DBConnection import DBConnection
|
||||
import logging
|
||||
import configparser
|
||||
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
|
||||
schueler = [
|
||||
{'name':'Simpson','vorname': 'Bart'},
|
||||
{'name':'Simpson','vorname':'Lisa'},
|
||||
{'name':'van Houten','vorname':'Milhouse'},
|
||||
{'name':'Wiggum','vorname':'Ralph'},
|
||||
{'name':'Jones','vorname':'Jimbo'}
|
||||
]
|
||||
|
||||
class TestDBConnection(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
logging.debug(f'setUpClass() {cls}')
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('db.ini')
|
||||
logging.debug(f"host={config['pidb']['host']}")
|
||||
|
||||
cls.db_connection = DBConnection()
|
||||
cls.db_connection.connect(config['pidb']['host'],config['pidb']['user'],config['pidb']['password'],config['pidb']['database'])
|
||||
if cls.db_connection.isConnected():
|
||||
logging.debug('erzeuge Table schueler...')
|
||||
cls.db_connection.execute_query('create table if not exists schueler (id int auto_increment primary key, vorname varchar(20),name varchar(20))')
|
||||
|
||||
# Testdaten aufbauen.
|
||||
cls.fill_table()
|
||||
logging.debug('setup() fertig')
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
logging.debug(f'tearDownClass() {cls}')
|
||||
|
||||
logging.debug('Räume Tables auf')
|
||||
cls.db_connection.execute_query('drop table if exists schueler')
|
||||
logging.debug('schliesse Datenbankverbindung.')
|
||||
cls.db_connection.disconnect()
|
||||
cls.db_connection = None
|
||||
|
||||
|
||||
# input('Press Any Key')
|
||||
|
||||
#Testdaten in die Datenbank übertragen
|
||||
@classmethod
|
||||
def fill_table(cls):
|
||||
for s in range(0,len(schueler)):
|
||||
student = schueler[s]
|
||||
cls.db_connection.insertSchueler(student)
|
||||
|
||||
# Nur die Records mit Nachname ='Simpson' lesen
|
||||
def test_select_name(self):
|
||||
self.assertTrue(self.db_connection.isConnected())
|
||||
|
||||
result = self.db_connection.execute_query("select * from schueler where name='Simpson'")
|
||||
self.assertEqual(2,len(result))
|
||||
|
||||
def test_select_all(self):
|
||||
# self.test_insert()
|
||||
result = self.db_connection.execute_query("select * from schueler")
|
||||
logging.debug(f'result={result}')
|
||||
self.assertEqual(len(schueler),len(result))
|
||||
|
||||
for i in range(len(result)):
|
||||
self.assertEqual(schueler[i]['vorname'], result[i][1])
|
||||
|
||||
def test_connected(self):
|
||||
self.assertTrue(self.db_connection.isConnected())
|
||||
|
||||
|
||||
def test_create_table(self):
|
||||
self.assertTrue(self.db_connection.isConnected())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ =='__main__':
|
||||
unittest.main()
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user