Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.5
- import mysql.connector #import library mysql.connector
- from mysql.connector import errorcode #import library errorcod from mysql.conn
- import csv #import standart library csv
- try:
- 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
- except mysql.connector.Error as err:
- if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
- print("Something is wrong with your user name or password")
- elif err.errno == errorcode.ER_BAD_DB_ERROR:
- print("Database does not exist")
- else:
- print(err)
- else:
- cursor = cnx.cursor() #create a new cursor
- query = ("SELECT date_format(date, '%d.%m.%Y %H:%i') as date, sensor, id, h FROM data_hydrolevel WHERE sensor = %s;")
- c = 23
- cursor.execute(query, c)
- #for (date, sensor, id, h) in cursor:
- #print(date, sensor, id, h)
- csvFile = open('sensor13.csv', 'w', newline='')
- writer = csv.writer(csvFile, delimiter=';')
- writer.writerows(cursor)
- #for i in cursor:
- #writer.writerow(i)
- #print(i[0], i[1], i[2], i[3], sep = ';')
- csvFile.close()
- cursor.close()
- cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement