Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 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. query = ("SELECT date_format(date, '%d.%m.%Y %H:%i') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = 24;")
  20. cursor.execute(query)
  21. #for (date, sensor, id, h) in cursor:
  22. #print(date, sensor, id, h)
  23.  
  24. csvFile = open('sensor24.csv', 'w', newline='')
  25. writer = csv.writer(csvFile, delimiter=';')
  26.  
  27. for i in cursor:
  28. writer.writerow(i)
  29. #print(i[0], i[1], i[2], i[3], sep = ';')
  30. csvFile.close()
  31. cursor.close()
  32. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement