Feb-22-2017, 11:01 PM
Hi there,
I have been creating an attendance system (Clocking in and out machine) for the company i work for. I have been storing all the data in a mysql server on the local sever at work. I am now trying to get it to write all the information for a particular month tp a csv and then store it on a network shared area.
This is the section of code i have for getting the data;
This is what im thinking for the print function;
my main question is would this work. Also how could i name the file in the month being exported ie 2/2017.csv?
I have never used any of the csv functions before and before doing this code i had never done any coding either so be nice with me.
Thankyou in advance for the help
I have been creating an attendance system (Clocking in and out machine) for the company i work for. I have been storing all the data in a mysql server on the local sever at work. I am now trying to get it to write all the information for a particular month tp a csv and then store it on a network shared area.
This is the section of code i have for getting the data;
1 2 3 4 5 6 7 8 9 10 11 12 13 |
print ( 'Print to CSV File' ) printmonth = input ( 'Month to Export to CSV (eg 2 for feb, 11 for nov)' ) export = "SELECT staff_name, date_, clock_in, clock_out WHERE Month_ = %s" cursor.execute(export, (printmonth)) results4 = cursor.fetchall() if not cursor.rowcount: print ( 'Error #4' ) print ( 'No Log on this date found' ) time.sleep( 2 ) restart_program() else : ##this is where the print function will go. print ( 'printed' ) |
1 2 3 4 5 |
rows = cursor.fetchall() fp = open ( '/tmp/file.csv' , 'w' ) ##different path but you get the idea myFile = csv.writer(fp, lineterminator = '\n' ) myFile.writerows(rows) fp.close() |
I have never used any of the csv functions before and before doing this code i had never done any coding either so be nice with me.
Thankyou in advance for the help