· 10 years ago · Apr 01, 2016, 12:48 AM
1import pymysql.cursors
2
3'''
4Connection er koblingen mot databasen. (inkludert commit og rollback)
5Cursor kjorer SQLene (mulig a ha flere cursors) execute() og fetchall()
6
7Ansatte
8+----+----------+-----+-----------+
9| ID | Stilling | Navn | Timelonn |
10+----+----------+-----+-----------+
11| 1 | Ramesh | 32 | Ahmedabad |
12| 2 | Khilan | 25 | Delhi |
13| 3 | kaushik | 23 | Kota |
14| 4 | Chaitali | 25 | Mumbai |
15| 5 | Hardik | 27 | Bhopal |
16| 6 | Komal | 22 | MP |
17| 7 | Muffy | 24 | |
18+----+----------+-----+-----------+
19
20Timer
21+----+----------+-----+--------------+
22| ID | Ansatt | Dag | Antall Timer |
23+----+----------+-----+--------------+
24| 1 | | 32 | Ahmedabad |
25| 2 | | 25 | Delhi |
26| 3 | kaushik | 23 | Kota |
27| 4 | Chaitali | 25 | Mumbai |
28| 5 | Hardik | 27 | Bhopal |
29| 6 | Komal | 22 | MP |
30| 7 | Muffy | 24 | |
31+----+----------+-----+--------------+
32'''
33
34class mySql_Connector():
35
36 # 1. Lag en modell av databasen
37 def __init__(self):
38 self.connection = pymysql.connect(host='localhost', user='root', password='passord99')
39 self.createTables(self.connection)
40 self.createViews(self.connection)
41
42 # Lager databasen
43 # Tabell for Ansatte: Stilling, Navn, timelonn.
44 # Tabell Timer: Dag, Antall timer, ansatt.
45 def createDatabaseTables(self, connection):
46 try:
47 with connection.cursor() as cursor:
48 sql = "CREATE DATABASE IF NOT EXISTS db"
49 cursor.execute(sql)
50 sql = "USE db"
51 cursor.execute(sql)
52 sql = "CREATE TABLE IF NOT EXISTS db.ansatte(ansattID int NOT NULL AUTO_INCREMENT PRIMARY KEY, stilling varchar(45), navn varchar(45), timelonn int)"
53 cursor.execute(sql)
54 sql = "CREATE TABLE IF NOT EXISTS db.timer(timeID int NOT NULL AUTO_INCREMENT PRIMARY KEY, dag varchar(45), antallTimer int, ansattID int, FOREIGN KEY(ansattID) REFERENCES(ansatte(ansattID)))"
55 cursor.execute(sql)
56 except Exception as e:
57 raise e
58
59 # Lager view: Liste over antall timer per ansatt.
60 def createViews(connection):
61 try:
62 with connection.cursor() as cursor:
63 sql = "CREATE OR REPLACE VIEW db.antallTimerPer AS SELECT navn, sum(antalltimer) as totalTimer FROM ansatte, timer WHERE ansatte.ansattID = timer.ansattID GROUP BY navn"
64 cursor.execute(sql)
65 cursor.close()
66 except Exception as e:
67 raise e
68
69 # 3. Lag funksjonalitet slik at brukeren kan skrive inn navn og timelonn som legges i ansatt tabell.
70 def addTo_Ansatt(self, args = []):
71 if not len(args):
72 return
73 try:
74 with self.connection.cursor() as cursor:
75 args = args.split( )
76 sql = "INSERT INTO db.ansatte values(NULL,'%s' ,'%s', '%s')" % (args[0], args[1], args[2])
77 cursor.execute(sql)
78 self.connection.commit()
79 cursor.close()
80 except Exception as e:
81 raise e
82
83 # 4. Lag funksjonalitet slik at brukeren kan sette inn dag, antall timer og ansatt i timer tabellen. Ansattnummer kan brukes som input.
84 def addTo_Timer(self, args = []):
85 if not len(args):
86 return
87 try:
88 with self.connection.cursor() as cursor:
89 args = args.split( )
90 sql = "INSERT INTO db.timer values(NULL,'%s' ,'%s', '%s')" % (args[0], args[1], args[2])
91 cursor.execute(sql)
92 self.connection.commit()
93 cursor.close()
94 except Exception as e:
95 raise e
96
97 def showAntallTimer(self):
98 try:
99 with self.connection.cursor() as cursor:
100 sql = "SELECT navn,totalTimer FROM db.antallTimerPer"
101 result = cursor.execute(sql)
102 cursor.close()
103 except Exception as e:
104 raise e
105 return cursor
106
107 # Materialisert view: Liste over hvor mye betalt hver ansatt skal ha per dag.
108 # 6. Lag funksjonalitet som oppdaterer det materialiserte viewet. Det kan f.eks. være et menyvalg som kjører oppdateringen. (krav IV).
109 # 7. Lag funksjonalitet som viser det materialiserte viewet
110
111
112
113if __name__ == '__main__':
114 connection = mySql_Connector()
115 raw = raw_input('Enter position, name and hours worked (seperate with spaces):')
116 connection.addTo_Ansatt(raw)
117 raw = raw_input('Enter dag, antalltimer and ansattID (seperate with spaces):')
118 connection.addTo_Timer(raw)
119
120 #Prints out all rows in the view antalltimer
121 for row in connection.showAntallTimer():
122 print row