Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- '''
- Connection er koblingen mot databasen. (inkludert commit og rollback)
- Cursor kjorer SQLene (mulig a ha flere cursors) execute() og fetchall()
- Ansatte
- +----+----------+-----+-----------+
- | ID | Stilling | Navn | Timelonn |
- +----+----------+-----+-----------+
- | 1 | Ramesh | 32 | Ahmedabad |
- | 2 | Khilan | 25 | Delhi |
- | 3 | kaushik | 23 | Kota |
- | 4 | Chaitali | 25 | Mumbai |
- | 5 | Hardik | 27 | Bhopal |
- | 6 | Komal | 22 | MP |
- | 7 | Muffy | 24 | |
- +----+----------+-----+-----------+
- Timer
- +----+----------+-----+--------------+
- | ID | Ansatt | Dag | Antall Timer |
- +----+----------+-----+--------------+
- | 1 | | 32 | Ahmedabad |
- | 2 | | 25 | Delhi |
- | 3 | kaushik | 23 | Kota |
- | 4 | Chaitali | 25 | Mumbai |
- | 5 | Hardik | 27 | Bhopal |
- | 6 | Komal | 22 | MP |
- | 7 | Muffy | 24 | |
- +----+----------+-----+--------------+
- '''
- class mySql_Connector():
- # 1. Lag en modell av databasen
- def __init__(self):
- self.connection = pymysql.connect(host='localhost', user='root', password='passord99')
- self.createTables(self.connection)
- self.createViews(self.connection)
- # Lager databasen
- # Tabell for Ansatte: Stilling, Navn, timelonn.
- # Tabell Timer: Dag, Antall timer, ansatt.
- def createDatabaseTables(self, connection):
- try:
- with connection.cursor() as cursor:
- sql = "CREATE DATABASE IF NOT EXISTS db"
- cursor.execute(sql)
- sql = "USE db"
- cursor.execute(sql)
- sql = "CREATE TABLE IF NOT EXISTS db.ansatte(ansattID int NOT NULL AUTO_INCREMENT PRIMARY KEY, stilling varchar(45), navn varchar(45), timelonn int)"
- cursor.execute(sql)
- 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)))"
- cursor.execute(sql)
- except Exception as e:
- raise e
- # Lager view: Liste over antall timer per ansatt.
- def createViews(connection):
- try:
- with connection.cursor() as cursor:
- 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"
- cursor.execute(sql)
- cursor.close()
- except Exception as e:
- raise e
- # 3. Lag funksjonalitet slik at brukeren kan skrive inn navn og timelonn som legges i ansatt tabell.
- def addTo_Ansatt(self, args = []):
- if not len(args):
- return
- try:
- with self.connection.cursor() as cursor:
- args = args.split( )
- sql = "INSERT INTO db.ansatte values(NULL,'%s' ,'%s', '%s')" % (args[0], args[1], args[2])
- cursor.execute(sql)
- self.connection.commit()
- cursor.close()
- except Exception as e:
- raise e
- # 4. Lag funksjonalitet slik at brukeren kan sette inn dag, antall timer og ansatt i timer tabellen. Ansattnummer kan brukes som input.
- def addTo_Timer(self, args = []):
- if not len(args):
- return
- try:
- with self.connection.cursor() as cursor:
- args = args.split( )
- sql = "INSERT INTO db.timer values(NULL,'%s' ,'%s', '%s')" % (args[0], args[1], args[2])
- cursor.execute(sql)
- self.connection.commit()
- cursor.close()
- except Exception as e:
- raise e
- def showAntallTimer(self):
- try:
- with self.connection.cursor() as cursor:
- sql = "SELECT navn,totalTimer FROM db.antallTimerPer"
- result = cursor.execute(sql)
- cursor.close()
- except Exception as e:
- raise e
- return cursor
- # Materialisert view: Liste over hvor mye betalt hver ansatt skal ha per dag.
- # 6. Lag funksjonalitet som oppdaterer det materialiserte viewet. Det kan f.eks. være et menyvalg som kjører oppdateringen. (krav IV).
- # 7. Lag funksjonalitet som viser det materialiserte viewet
- if __name__ == '__main__':
- connection = mySql_Connector()
- raw = raw_input('Enter position, name and hours worked (seperate with spaces):')
- connection.addTo_Ansatt(raw)
- raw = raw_input('Enter dag, antalltimer and ansattID (seperate with spaces):')
- connection.addTo_Timer(raw)
- #Prints out all rows in the view antalltimer
- for row in connection.showAntallTimer():
- print row
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement