Guest User

Untitled

a guest
Mar 6th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. __author__ = 'robdr'
  2.  
  3. import mysql.connector
  4.  
  5.  
  6. class Data(object) :
  7.     # set connector=
  8.     def __init__(self) :
  9.         super(Data, self).__init__()
  10.         try :
  11.             self.conn = mysql.connector.connect(host='localhost', database='db', user='root', password='')
  12.         except :
  13.             print('Failed to connect<br>')
  14.             return
  15.         self.cursor = self.conn.cursor()
  16.         pass
  17.  
  18.     def __del__(self) :
  19.         self.cursor.close()
  20.         self.conn.close()
  21.         pass
  22.  
  23.     # where class=
  24.     def get_by_classname(self, cn) :
  25.         if cn != '*' :
  26.             cn = "students.class = '" + cn + "'"
  27.         else :
  28.             cn = "1"
  29.         self.cursor.execute("SELECT students.name, students.state, e1.time, e1.date, e2.time, e2.date FROM students, events AS e1, events AS e2 WHERE e1.id = students.linkIn AND e2.id = students.linkOut AND " + cn)
  30.  
  31.         return self.cursor.fetchall()
  32.  
  33.     # where time=
  34.     def get_by_datetime(self, dt) :
  35.         # parse dt
  36.         if (dt != '*') :
  37.             dt = "events.date = '" + dt + "'"
  38.         else :
  39.             dt = "1"
  40.         self.cursor.execute(
  41.             "SELECT students.name, students.class, events.time, events.type, events.comment FROM students, events WHERE students.id = events.idStudent and " + dt)
  42.         return self.cursor.fetchall()
  43.  
  44.     # where name=
  45.     def get_by_name(self, name) :
  46.         self.cursor.execute(
  47.             "SELECT events.date, events.time, events.type, events.comment FROM students, events WHERE students.name = '" + name + "' and students.id = events.idStudent")
  48.         return self.cursor.fetchall()
  49.  
  50.     # get id=
  51.     def get_id_by_hash(self, hash) :
  52.         # SELECT id from students WHERE hash=1234
  53.         self.cursor.execute("SELECT id from students WHERE hash = " + str(hash))
  54.         return self.cursor.fetchone()[0]
  55.  
  56.     # insert new event
  57.     def insert_event(self, idStudent, type, comment) :
  58.         import datetime
  59.         now = datetime.datetime.now()
  60.         query = "INSERT INTO `events`(`idStudent`, `date`, `time`, `type`, `comment`) VALUES ( %s, %s, %s, %s, %s)"
  61.         data = (str(idStudent), now.strftime("%Y-%m-%d"), now.strftime("%H:%M"), str(type), str(comment))
  62.         self.cursor.execute(query, data)
  63.         self.conn.commit()
  64.         query = "UPDATE `students` SET `state`=%s, "  + ("`linkIn`=%s" if (type == 0) else "`linkOut`=%s") + " WHERE id=" + str(idStudent)
  65.         data = (str(type), str(self.cursor.lastrowid))
  66.         self.cursor.execute(query, data)
  67.         self.conn.commit()
  68.  
  69.     #insert student
  70.     def insert_student(self, name, hash, cl) :
  71.         query = "INSERT INTO `students` (`name`, `hash`, `class`, `state`) VALUES ( %s, %s, %s, %s)"
  72.         data = (str(name), str(hash), str(cl), '0')
  73.         self.cursor.execute(query, data)
  74.         self.conn.commit()
Add Comment
Please, Sign In to add comment