def find_by_id(self, id): """Suchen eines Studenten nach der übergebenen ID. :param id Primärschlüsselattribut eines Studenten aus der Datenbank :return Student-Objekt, welche mit der ID übereinstimmt, None wenn kein Eintrag gefunden wurde """ result = None cursor = self._connection.cursor() command = "SELECT id, name, email, google_user_id, mat_nr, kuerzel FROM studenten WHERE id='{}'".format( id) cursor.execute(command) tuples = cursor.fetchall() try: (id, name, email, google_user_id, mat_nr, kuerzel) = tuples[0] student = Student() student.set_id(id) student.set_name(name) student.set_email(email) student.set_google_user_id(google_user_id) student.set_mat_nr(mat_nr) student.set_kuerzel(kuerzel) result = student except IndexError: """Der IndexError wird oben beim Zugriff auf tuples[0] auftreten, wenn der vorherige SELECT-Aufruf keine Tupel liefert, sondern tuples = cursor.fetchall() eine leere Sequenz zurück gibt.""" result = None self._connection.commit() cursor.close() return result
def find_by_mail(self, email): result = None cursor = self._cnx.cursor() command = "SELECT * FROM student WHERE email LIKE '%{}%'".format(email) cursor.execute(command) tuples = cursor.fetchall() try: (id, creation_time, name, matriculation_nr, course_abbr, email, google_user_id) = tuples[0] student = Student() student.set_id(id) student.set_creation_time(creation_time) student.set_name(name) student.set_matriculation_nr(matriculation_nr) student.set_course_abbr(course_abbr) student.set_email(email) student.set_google_user_id(google_user_id) result = student except IndexError: result = None self._cnx.commit() cursor.close() return result
def find_all(self): """Auslesen aller Studenten aus der Datenbank :return Alle Studenten-Objekte im System """ result = [] cursor = self._connection.cursor() cursor.execute("SELECT * from studenten") tuples = cursor.fetchall() for (id, name, email, google_user_id, rolle, mat_nr, kuerzel) in tuples: student = Student() student.set_id(id) student.set_name(name) student.set_email(email) student.set_google_user_id(google_user_id) student.set_rolle(rolle) student.set_mat_nr(mat_nr) student.set_kuerzel(kuerzel) result.append(student) self._connection.commit() cursor.close() return result