Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- conn=mysql.connector.connect(user='jump',password='secret',host='localhost',database='accidents')
- cursor=conn.cursor()
- cursor.execute("SELECT vtype FROM vehicle_type WHERE vtype LIKE '%otorcycle%';")
- cycleList = cursor.fetchall()
- selectSQL = ("""
- SELECT t.vtype, a.accident_severity
- FROM accidents_2016 AS a
- JOIN vehicles_2016 AS v ON a.accident_index = v.accident_index
- JOIN vehicle_type AS t ON v.vehicle_type = t.vcode
- WHERE t.vtype LIKE %s
- ORDER BY a.accident_severity;""")
- insertSQL = ("""INSERT INTO accident_medians VALUES (%s, %s);""")
- for cycle in cycleList:
- cursor.execute(selectSQL,cycle[0])
- accidents = cursor.fetchall()
- quotient, remainder = divmod(len(accidents),2)
- if remainder:
- med_sev = accidents[quotient][1]
- else:
- med_sev = (accidents[quotient][1] + accidents[quotient+2][1])/2
- print('Finding median for',cycle[0])
- cursor.execute(insertSQL,(cycle[0],med_sev))
- conn.commit()
- conn.close()
Add Comment
Please, Sign In to add comment