Guest User

Untitled

a guest
Jul 6th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import mysql.connector
  2. conn=mysql.connector.connect(user='jump',password='secret',host='localhost',database='accidents')
  3. cursor=conn.cursor()
  4. cursor.execute("SELECT vtype FROM vehicle_type WHERE vtype LIKE '%otorcycle%';")
  5. cycleList = cursor.fetchall()
  6. selectSQL = ("""
  7. SELECT t.vtype, a.accident_severity
  8. FROM accidents_2016 AS a
  9. JOIN vehicles_2016 AS v ON a.accident_index = v.accident_index
  10. JOIN vehicle_type AS t ON v.vehicle_type = t.vcode
  11. WHERE t.vtype LIKE %s
  12. ORDER BY a.accident_severity;""")
  13. insertSQL = ("""INSERT INTO accident_medians VALUES (%s, %s);""")
  14.  
  15. for cycle in cycleList:
  16. cursor.execute(selectSQL,cycle[0])
  17. accidents = cursor.fetchall()
  18. quotient, remainder = divmod(len(accidents),2)
  19. if remainder:
  20. med_sev = accidents[quotient][1]
  21. else:
  22. med_sev = (accidents[quotient][1] + accidents[quotient+2][1])/2
  23. print('Finding median for',cycle[0])
  24. cursor.execute(insertSQL,(cycle[0],med_sev))
  25. conn.commit()
  26. conn.close()
Add Comment
Please, Sign In to add comment