diff --git a/Gremlin_Python/Gremlin_Python.py b/Gremlin_Python/Gremlin_Python.py index 9d679cb..e463f77 100644 --- a/Gremlin_Python/Gremlin_Python.py +++ b/Gremlin_Python/Gremlin_Python.py @@ -1,4 +1,6 @@ import mysql.connector as database + +print("Willkommen bei Gremlin_Python!") connection = database.connect( host="woitschetzki.de", @@ -9,8 +11,7 @@ connection = database.connect( cursor = connection.cursor() - -def getContact(lastName): +def readContactFromDb(lastName): try: statement = "SELECT ContactId, AccountId, FirstName, LastName, Gender, Email FROM Contacts WHERE LastName LIKE %s" data = (lastName,) @@ -27,12 +28,45 @@ def getContact(lastName): except database.Error as error: print(f"Error retrieving entry from database: {error}") +def writeContactToDb(accountId, firstName, lastName, gender, email): + try: + statement = "INSERT INTO Contacts (accountId, firstName, lastName, gender, email) VALUES (%s, %s, %s, %s, %s)" + data = (accountId, firstName, lastName, gender, email) + cursor.execute(statement, data) + connection.commit() + print(f"Successfully added {firstName} {lastName} to database.") + except database.Error as error: + print(f"Error adding entry to database: {error}") -print("Willkommen bei Gremlin_Python!") +def createNewContact(): + print("AccountID?") + accountId = input() + + print("FirstName?") + firstName = input() + + print("LastName?") + lastName = input() + + print("Gender?") + gender = input() + + print("Email?") + email = input() + + writeContactToDb(accountId, firstName, lastName, gender, email) + +def main(): + while 2 > 1: + print("Nach welchem Nachnamen suchen Sie?:") + contactSearch = input() + readContactFromDb(f"%{contactSearch}%") + + print("Neuen Kontakt hinzufügen? (j/N):") + wantNewContact = input() + if wantNewContact == "j": + createNewContact() -while 2 > 1: - print("Nach welchem Kunden suchen Sie?: ") - contactSearch = input() - getContact(f"%{contactSearch}%") +main() -connection.close() +connection.close() \ No newline at end of file diff --git a/Gremlin_Python/sqlalchemy.py b/Gremlin_Python/sqlalchemy.py new file mode 100644 index 0000000..273f2b9 --- /dev/null +++ b/Gremlin_Python/sqlalchemy.py @@ -0,0 +1,29 @@ +from sqlalchemy.ext.automap import automap_base +from sqlalchemy.orm import Session +from sqlalchemy import create_engine + +Base = automap_base() + +# engine, suppose it has two tables 'user' and 'address' set up +engine = create_engine( + "mysql+mysqldb://sascha:mgltoJtmmDnKJ86LltsGdw@server0:3306/regulus") + +# reflect the tables +Base.prepare(autoload_with=engine) + +# mapped classes are now created with names by default +# matching that of the table name. +firstName = Base.classes.FirstName +lastName = Base.classes.LastName + +session = Session(engine) + +# rudimentary relationships are produced +session.add(Address(email_address="thomas.deckstein@kuraray.com", + user=User(name="Deckstein"))) +session.commit() + +# collection-based relationships are by default named +# "_collection" +u1 = session.query(User).first() +print(u1.address_collection)