Last active
January 9, 2019 04:02
-
-
Save zealinux/96c11d3020e4c475f27b1b8f8f55cd27 to your computer and use it in GitHub Desktop.
mysql data to csv
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', db='dbname') | |
cur = db.cursor() | |
cur.execute(QUERY) | |
result = cur.fetchall() | |
c = csv.writer(open('dbdump01.csv', 'wb')) | |
for x in result: | |
c.writerow(x) |
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
SELECT order_id,product_name,qty | |
FROM orders | |
WHERE foo = 'bar' | |
INTO OUTFILE '/tmp/orders.csv' | |
FIELDS TERMINATED BY ',' | |
ENCLOSED BY '"' | |
LINES TERMINATED BY '\n'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment