Last active
October 12, 2022 11:00
-
-
Save ruanbekker/f4c5d917dd4d1b777b39c86240948dd2 to your computer and use it in GitHub Desktop.
Dump MySQL Data to CSV with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirement: python-mysqldb | |
import MySQLdb as dbapi | |
import sys | |
import csv | |
QUERY='SELECT * FROM mydb.people;' | |
db=dbapi.connect(host='localhost',user='root',passwd='password') | |
cur=db.cursor() | |
cur.execute(QUERY) | |
result=cur.fetchall() | |
c = csv.writer(open('dbdump01.csv', 'wb')) | |
for x in result: | |
c.writerow(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result = DB.query('select * from fb')
c = csv.writer(open('dbdump01.csv', 'w'))
for x in result:
c.writerow(x)
This works cool