sergkh

Untitled

Nov 14th, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. """Example project for ouda."""
  2. import mysql.connector
  3.  
  4. mysqldb = mysql.connector.connect(
  5.     host="localhost", user="root", password="password", database="ouda"
  6. )
  7.  
  8. mycursor = mysqldb.cursor()
  9.  
  10.  
  11. class Student:
  12.     """main class for students"""
  13.  
  14.     def __init__(self, name, mail_address, gender):
  15.         """Create a object of the student class.
  16.        Parameters
  17.        ----------
  18.        name: str
  19.        The name of the student
  20.        mail_address: str
  21.        The email address of the students
  22.        gender: gender accept values M or F
  23.        """
  24.         self.student_id = 0
  25.         self.name = name
  26.         self.mail_address = mail_address
  27.         self.gender = gender
  28.  
  29.     def __str__(self):
  30.         return " id: %s name: %s " % (self.student_id, self.name)
  31.  
  32.     def get_student_id(self):
  33.         """Return id from the student table for existing student object"""
  34.         sql = (
  35.             "SELECT student_id FROM student WHERE name='%s' AND mail_address='%s' AND gender='%s'"
  36.             % (self.name, self.mail_address, self.gender)
  37.         )
  38.         print(sql)
  39.         mycursor.execute(sql)
  40.         return mycursor.fetchone()[0]
  41.  
  42.     def save(self):
  43.  
  44.         if self.student_id == 0:
  45.             # we have not saved a student yet
  46.             sql = (
  47.                 """INSERT INTO student (name, mail_address, gender) VALUES('%s', '%s', '%s')"""
  48.                 % (self.name, self.mail_address, self.gender)
  49.             )
  50.  
  51.         else:
  52.             # Record is presend already we need to update it only.
  53.             sql = """UPDATE student SET name=%s WHERE student_id="%s" """ % (
  54.                 self.name,
  55.                 self.student_id,
  56.             )
  57.         print(sql)
  58.         mycursor.execute(sql)
  59.         mysqldb.commit()
  60.         self.student_id = self.get_student_id()
  61.  
  62.  
  63. def studentlist():
  64.     """Return existing student list."""
  65.     sql = """SELECT * from student"""
  66.     mycursor.execute(sql)
  67.     recordlist = mycursor.fetchall()
  68.     StudentList = []
  69.     for student in recordlist:
  70.         studentobj = Student(student[1], student[2], student[3])
  71.         studentobj.student_id = student[0]
  72.         StudentList.append(studentobj)
  73.     return StudentList
  74.  
  75.  
  76. def main():
  77.     """Show simple sorting."""
  78.     for student in studentlist():
  79.         print(student)
  80.     newstudent = Student("Mike", "[email protected]", "M")
  81.     newstudent.save()
  82.  
  83.  
  84. if __name__ == "__main__":
  85.     main()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment