Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql csv
#1
Hello,

First of all I am a total newbie as this are my first python coding...

I am trying to write a python script to extract data from mysql tatabase to a csv.
The data I want are in a single table except for user first and last name. Therefore in the csv, I would like to change the user id by the first and last name. I can do this with the following code but I cannot write the final csv properly. Could any one help ? I would also apreciate if anyone has a better way of coding this

thank you

#!/usr/bin/python

import csv
import pymysql
import shutil

mydb = pymysql.connect(host='localhost',
    user='maths',
    passwd='maths-maths',
    db='maths')
cursor = mydb.cursor()

## query
queryall = ("SELECT * FROM wp_h5p_results ")
cursor.execute(queryall)

### write to csv file
csv_temp = csv.writer(open("temp.csv", "wt")) # create csv
csv_temp.writerow(["id","activité","userid","score","score_max","commencé","terminé","durée"]) # write headers
csv_temp.writerows(cursor) # write records
del csv_temp # close csv file
with open('temp.csv') as csvinput:
    with open('score.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput)
        next(csvinput)
        for row in csv.reader(csvinput):
            queryfirstname = ("select meta_value from wp_usermeta where meta_key='first_name' and user_id = %s ;"%row[2])
            cursor.execute(queryfirstname)
            name = cursor.fetchall()
            row.append(name[0][0])
            querylastname = ("select meta_value from wp_usermeta where meta_key='last_name' and user_id = %s ;"%row[2])
            cursor.execute(querylastname)
            name = cursor.fetchall()
            row.append(name[0][0])
            print(row)
        writer.writerows(row) # write records

            

del writer # close csv file
cursor.close()
mydb.close()
Reply
#2
you need to also close csv_temp otherwise your buffers may not be written.
Reply
#3
Hello, Thank you for your advice, I have closed all the files but still the score.csv is incorrect.

Is there a python tool to extract data from mysql to replace or add columns in csv files?

Sincerely,
Reply
#4
I don't use mysql, but every DBMS has an admin program, including MySQL, you can download here: https://dev.mysql.com/downloads/workbench/
Reply
#5
writer.writerows() takes a sequence of rows as its parameter. Use writer.writerow(row) instead, and indent it more to put it in the 'for row ...' block.
Reply
#6
Great thanks it now works !!!

#!/usr/bin/python

import csv
import pymysql
import shutil

mydb = pymysql.connect(host='localhost',
    user='maths',
    passwd='maths-maths',
    db='maths')
cursor = mydb.cursor()

## query
queryall = ("SELECT * FROM wp_h5p_results ")
cursor.execute(queryall)

### write to csv file
csv_temp = csv.writer(open("temp.csv", "wt")) # create csv
csv_temp.writerows(cursor) # write records
del csv_temp # close csv file
with open('temp.csv') as csvinput:
    with open('score.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput)
        writer.writerow(["id","activité","userid","score","score_max","commencé","terminé","durée","prenom","nom"]) # write headers
        next(csvinput)
        for row in csv.reader(csvinput):
            queryfirstname = ("select meta_value from wp_usermeta where meta_key='first_name' and user_id = %s ;"%row[2])
            cursor.execute(queryfirstname)
            name = cursor.fetchall()
            row.append(name[0][0])
            querylastname = ("select meta_value from wp_usermeta where meta_key='last_name' and user_id = %s ;"%row[2])
            cursor.execute(querylastname)
            name = cursor.fetchall()
            row.append(name[0][0])
            print(row)
            writer.writerow(row) # write records

del csvinput # close csv file
del csvoutput # close csv file
del writer # close csv file
cursor.close()
mydb.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 716 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,203 Feb-09-2022, 09:55 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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