Python Forum
Python 3 mysql export to csv
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 mysql export to csv
#1
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;
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')
This is what im thinking for the print function;
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()
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
Reply


Messages In This Thread
Python 3 mysql export to csv - by elliott0184 - Feb-22-2017, 11:01 PM
RE: Python 3 mysql export to csv - by zivoni - Feb-22-2017, 11:50 PM
RE: Python 3 mysql export to csv - by buran - Feb-23-2017, 04:19 AM
RE: Python 3 mysql export to csv - by joe_anonimist - Feb-23-2017, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 827 Oct-03-2023, 10:25 PM
Last Post: lostintime
Question Export Python output to Excel skyline1397 1 2,151 Jun-26-2022, 05:10 AM
Last Post: skyline1397
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,673 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,901 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python and MySql ogautier 8 3,535 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 3,351 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  How to export from Python to Excel? jpy 4 6,494 Dec-23-2020, 03:26 PM
Last Post: jpy
  Skeleton file export error Python Code pepapoha 4 3,636 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Python MySQL ogautier 0 2,161 Sep-03-2020, 03:54 PM
Last Post: ogautier
  Insert into mysql through python LaKhWaN 0 2,000 Aug-26-2020, 04:54 AM
Last Post: LaKhWaN

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020