Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. class database:
  2.     '''Database Class, loads data from database.db and creates
  3.    People objects.'''
  4.     def __init__(self, mode):
  5.         conn = sqlite3.connect("database.db")
  6.         cursor = conn.cursor()
  7.         if mode == True: #teacher
  8.             self.mode = 'teachers'
  9.             cursor.execute("""CREATE TABLE IF NOT EXISTS teachers (ID INTEGER PRIMARY KEY AUTOINCREMENT, FirstName varchar(30),
  10. Surname varchar (30), Training boolean, Days INTEGER, Children varchar)""") #creates table
  11.             conn.commit()
  12.         else: #children
  13.             self.mode = 'children'
  14.             cursor.execute("""CREATE TABLE IF NOT EXISTS children (ID INTEGER PRIMARY KEY AUTOINCREMENT, FirstName varchar(30),
  15. MiddleName varchar(30), Surname varchar (30), DateOfBirth date, requirements boolean, Days INTEGER)""") #creates table
  16.             conn.commit()
  17.         try:
  18.             self.activelist = self.loadPeople()
  19.         except:
  20.             print(self.mode,"Active List not loaded")
  21.             pass
  22.  
  23.     def printTable(self):
  24.         '''Prints table for debug'''
  25.         if self.mode:
  26.             print("ID - Firstname - Surname - Trained - Days") #teacher
  27.         else:
  28.             print("ID - Firstname - Middlename - Surname - Date of Birth - Requirments - Days")
  29.         conn = sqlite3.connect("database.db")
  30.         cursor = conn.cursor()
  31.         string = "SELECT * FROM " + self.mode
  32.         cursor.execute(string)
  33.         string = cursor.fetchall()
  34.         for i in range(len(list(string))):
  35.             print(string[i])
  36.         conn.commit()
  37.         return string
  38.  
  39.     def wipeTable(self):
  40.         print('Are you sure you want to delete the data in')
  41.         print('the table ',self.mode,'? This will be permanent.',sep='')
  42.         answer = input('Y/N: ').lower()
  43.         if answer == 'y' or answer == 'yes':
  44.             conn = sqlite3.connect("database.db")
  45.             cursor = conn.cursor()
  46.             string = "DELETE FROM " + self.mode
  47.             print(string)
  48.             cursor.execute(string)
  49.             cursor.execute("""VACUUM""")
  50.             print("Data deleted")
  51.             conn.commit()
  52.         else:
  53.             print("Data NOT deleted")
  54.  
  55.     def createAddString(self, person):
  56.         '''Creates SQL String'''
  57.         #print('mode =',self.mode)
  58.         if self.mode == 'children':
  59.             string = "INSERT INTO " + self.mode + " VALUES (NULL, '" + person.firstname + "', '" + person.middlename + "', '" + person.surname + "', '" + person.dob.strftime('%Y-%m-%d') + "', '" + str(person.requirements) + "', '" + str(person.days) + "')"                        
  60.         else:
  61.             try:
  62.                 int(person.days)
  63.             except:
  64.                 print("why not")
  65.             person.printInfo()
  66.             string = "INSERT INTO " + self.mode + " VALUES (NULL, '" + person.firstname + "', '" + person.surname + "', '" + str(person.requirements)  + "', '" + str(person.days) + "', '" + str(person.Children).replace("'","") + "')"
  67.         #print(string)
  68.         return string
  69.  
  70.     def createQueryString(self, ID = None):
  71.         '''Creates SQL String'''
  72.         print('mode =',self.mode)
  73.         #if self.mode == 'children':
  74.             #string = "SELECT * FROM " + self.mode + " WHERE
  75.         #else:
  76.             #string = "SELECT
  77.         return string
  78.  
  79.     def addPerson(self, person, string = None):
  80.         '''Takes a Person class object (child/teacher) and stores them'''
  81.         conn = sqlite3.connect("database.db")
  82.         cursor = conn.cursor()
  83.         if string == None:
  84.             person.printInfo()
  85.             int(person.days)
  86.             string = self.createAddString(person)
  87.         cursor.execute(string)
  88.         conn.commit()
  89.  
  90.     def readPerson(self, ID = None):
  91.         '''Reads data from the database and creates a Person class object'''
  92.         conn = sqlite3.connect("database.db")
  93.         cursor = conn.cursor()
  94.         if ID != None:
  95.             string = 'SELECT * FROM ' + self.mode + ' WHERE ID = "' + str(ID) +'"'
  96.             #print('String: ',string)
  97.             cursor.execute(string)
  98.             result = cursor.fetchall()
  99.         else:
  100.             string = 'SELECT * FROM ' + self.mode
  101.             cursor.execute(string)
  102.             result = cursor.fetchall()
  103.         conn.commit()
  104.         for i in range(len(result)):
  105.             result[i] = list(result[i])
  106.         return result
  107.  
  108.     def loadPeople(self):
  109.         '''Loads Person Classes from this database, returns a list of objects'''
  110.         people = list()
  111.         if self.mode == 'children':
  112.             temp = self.readPerson()
  113.             #print('len:',len(temp))
  114.             for i in range(len(temp)):
  115.                 tempload = temp[i]
  116.                 #print(tempload)
  117.                 try:
  118.                     tempchild = Child(tempload[1],tempload[2],tempload[3],int(tempload[4][0:4]),int(tempload[4][5:7]),int(tempload[4][8:10]),bool(tempload[5]),tempload[6],tempload[0])
  119.                     people.append(tempchild)
  120.                 except:
  121.                     print('no')
  122.                     return temp, tempload
  123.  
  124.         else: #loadteachers
  125.             temp = self.readPerson()
  126.             #print('len:',len(temp))
  127.             for i in range(len(temp)):
  128.                 tempload = temp[i]
  129.                 print(tempload)
  130.                 tempteacher = Teacher(tempload[1],tempload[2],bool(tempload[3]),tempload[0],tempload[4],tempload[5])
  131.                 people.append(tempteacher)
  132.         return people
  133.  
  134.     def returnPerson(self, ID):
  135.         '''Returns a person from activelist based on ID'''
  136.         for i in range(len(self.activelist)):
  137.             if self.activelist[i].ID == ID:
  138.                 return self.activelist[i]
  139.  
  140.     def printInfo(self):
  141.         '''Debugging tool to print Person data'''
  142.         print(vars(self))
  143.  
  144.     def listPeople(self):
  145.         '''Prints a numbered list of people'''
  146.         for i in range(len(self.activelist)):
  147.             tp = self.activelist[i] #tp = tempperson
  148.             print(i+1,". ID:",tp.ID," ",tp.firstname," ",tp.surname,sep = '')
  149.         print()
  150.  
  151.     def deletePerson(self, ID):
  152.         conn = sqlite3.connect("database.db")
  153.         cursor = conn.cursor()
  154.         string = "DELETE FROM " + self.mode + " WHERE ID = " + str(ID)
  155.         print(string)
  156.         cursor.execute(string)
  157.         conn.commit()
  158.  
  159.     def editPerson(self, changing, person, choice):
  160.         '''Creates SQL string + edits person'''
  161.         l = ['FirstName','MiddleName','Surname','day','month','year','days','requirements','Days']
  162.         print("changing:",person.ID,person.firstname)
  163.         string = 'UPDATE ' + self.mode + " SET "
  164.         if self.mode == 'children': #children
  165.             if choice - 1 in [0,1,2,7,8]:
  166.                 string =+ l[choice-1]
  167.         string += " WHERE ID=" + person.ID
  168.         print("string")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement