Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #! /usr/bin/env python3.5
  2.  
  3. import csv # import standart library csv
  4. import mysql.connector # import library mysql.connector
  5. from mysql.connector import errorcode # import library errorcod from mysql.conn
  6.  
  7.  
  8. # db configuration
  9. DB_CONFIG = dict(
  10. user='root',
  11. password='t00r0DGn1',
  12. host='localhost',
  13. database='odgn'
  14. )
  15.  
  16. SENSOR_IDS = range(23, 45)
  17.  
  18. try:
  19. cnx = mysql.connector.connect(**DB_CONFIG) # open connection to the MySQL server and store the conn object in the variable cnx
  20.  
  21. except mysql.connector.Error as err:
  22. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  23. raise Exception("Something is wrong with your user name or password")
  24.  
  25. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  26. raise Exception("Database does not exist")
  27.  
  28. else:
  29. raise Exception(err)
  30.  
  31.  
  32. cursor = cnx.cursor() # create a new cursor
  33.  
  34. for sensor_id in SENSOR_IDS:
  35.  
  36. query = "SELECT date_format(date, ''"'%Y-%m-%d %H:%i:%S'"'') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = '%s';"
  37. cursor.execute(query, (sensor_id,))
  38.  
  39. with open('file_sensor_%s.dat' % sensor_id, 'a') as f:
  40. writer = csv.writer(f, delimiter=';')
  41. writer.writerows(cursor)
  42.  
  43. cursor.close()
  44.  
  45. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement