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
#2
With right indentation and probably with (printmonth,) instead of (printmonth) it should work, but you need to write your results4, not rows, the cursor will be empty after first fetchall(). You can even pass a cursor directly to csv.writer without using fetchall() at all - just myFile.writerows(cursor).

You can prepare your filename with string formatting (% or .format()) :

filename = "/tmp/data_{:d}_{:02d}.csv".format(2017, 2)

fp = open(filename, 'w')
Reply
#3
You need to specify also the year, not just the month. Once you have data for more than one year it will be a problem if you don't store also the year in the DB. Whether it will be a separate column, just the year or you will store full date (e.g. last day of the month) is your choice and db-design issue. For the file name you would probably use datetime.strftime()
Reply
#4
Another option would be to export your data directly from your sql query. Here is a nice tutorial http://www.mysqltutorial.org/mysql-export-table-to-csv/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 667 Oct-03-2023, 10:25 PM
Last Post: lostintime
Question Export Python output to Excel skyline1397 1 2,034 Jun-26-2022, 05:10 AM
Last Post: skyline1397
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,999 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,673 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python and MySql ogautier 8 3,339 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 3,220 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  How to export from Python to Excel? jpy 4 6,349 Dec-23-2020, 03:26 PM
Last Post: jpy
  Skeleton file export error Python Code pepapoha 4 3,484 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Python MySQL ogautier 0 2,101 Sep-03-2020, 03:54 PM
Last Post: ogautier
  Insert into mysql through python LaKhWaN 0 1,941 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