EmployeeDB/source/DataBank/fillDatabank.py

72 lines
2.4 KiB
Python

import sqlalchemy as db #somehow pyodbc does not work in container
from sqlalchemy.sql import text
from sqlalchemy_utils import database_exists, create_database
import numpy as np
'''
inputs a new Mitarbeiter into our databank
IN:
cursor: the cursor/engine which is the connection object to the db
newMitarbeiter: Array(7) with kuerzel, vorName, nachName, geschaeftstelle, eintrittsdatum
'''
def inputMitarbeiter(cursor, newMitarbeiter):
#write the SQL command to add data to our table
#sqlalchemy needs text(SQL command), and ' around the values
#tableName (attributes) values (of attributes)
input = text("""INSERT INTO "Mitarbeiter"
(kuerzel, vorName, nachName, geschaeftsStelle, eintrittsDatum, eMail, telefonNummer)
VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}')""" #need ' ' around string for SQL. Python does not preserve '' from string input!
.format(newMitarbeiter[0], newMitarbeiter[1], newMitarbeiter[2], newMitarbeiter[3], newMitarbeiter[4], newMitarbeiter[5], newMitarbeiter[6]))
cursor.execute(input)
cursor.commit()
'''
deletes a Mitarbeiter who is not working at crossnative anymore
IN:
cursor: the cursor/engine which is the connection object to the db
oldMitarbeiter: scalar string
'''
def deleteMitarbeiter(cursor, oldMitarbeiter):
input = text("""DELETE
FROM "Mitarbeiter"
WHERE kuerzel = '{}'""".format(oldMitarbeiter))
cursor.execute(input)
cursor.commit()
'''
checks which Mitarbeiter in DB are not in list of Mitarbeiter anymore
IN:
cursor: the cursor/engine which is the connection object to the db
currentMitarbeiter: Array(n)
OUT:
oldMitarbeiter. Array(m)
'''
def checkMitarbeiter(cursor, currentMitarbeiter):
oldMitarbeiter = []
input = text("""Select kuerzel
FROM "Mitarbeiter" """)
inDbMitarbeiter = cursor.execute(input)
#boolean array with True in [n] if inDbMitarbeiter[n] is in currentMitarbeiter
isIn = np.isin(inDbMitarbeiter, currentMitarbeiter)
#adds kurzel to oldMitarbeiter if in DB but not in Dobby(currentMitarbeiter)
for i in range(len(inDbMitarbeiter)):
if not (isIn[i]):
oldMitarbeiter.append(inDbMitarbeiter[i])
return oldMitarbeiter
if __name__ == '__main__':
pass