Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
76
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. query = ("SELECT date_format(date, '%d.%m.%Y %H:%i') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = %s;")
  20. c = 23
  21. cursor.execute(query, c)
  22. #for (date, sensor, id, h) in cursor:
  23. #print(date, sensor, id, h)
  24.  
  25. csvFile = open('sensor13.csv', 'w', newline='')
  26. writer = csv.writer(csvFile, delimiter=';')
  27.  
  28. writer.writerows(cursor)
  29. #for i in cursor:
  30. #writer.writerow(i)
  31. #print(i[0], i[1], i[2], i[3], sep = ';')
  32. csvFile.close()
  33. cursor.close()
  34. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement