Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. #! /usr/bin/env python3.5
  2.  
  3. import mysql.connector #import library mysql.connector
  4. from mysql.connector import errorcode #import library errorcod from mysql.conn
  5. import csv #import standart library csv
  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, 35)
  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, '%d.%m.%Y %H:%i') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = '%s';"
  37.     cursor.execute(query, (sensor_id,))
  38.  
  39.     with open('file_sensor_%s.scv' % sensor_id, 'w') 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