Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 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. try:
  8. cnx = mysql.connector.connect(user='root', password='t00r0DGn1', host='localhost', database='odgn') #open connection to the MySQL server and store the conn object in the variable cnx
  9. except mysql.connector.Error as err:
  10. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  11. print("Something is wrong with your user name or password")
  12. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  13. print("Database does not exist")
  14. else:
  15. print(err)
  16. else:
  17. cursor = cnx.cursor() #create a new cursor
  18.  
  19. for i in range(23, 35):
  20. query = ("SELECT date_format(date, '%d.%m.%Y %H:%i') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = '%s';")
  21. cursor.execute(query, (i,))
  22. csvFile = open('file_sensor_%s.scv' % i, 'w', newline='')
  23. writer = csv.writer(csvFile, delimiter=';')
  24. writer.writerows(cursor)
  25. #for row in cursor:
  26. #print(row[0], row[1], row[2], row[3], sep = ';')
  27. #writer.writerow(row)
  28.  
  29. csvFile.close()
  30. cursor.close()
  31. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement