You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
46 lines
1.5 KiB
1 year ago
|
#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 Werten 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.
|