Skip to content

Instantly share code, notes, and snippets.

@erincerys
Created September 26, 2013 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincerys/6717083 to your computer and use it in GitHub Desktop.
Save erincerys/6717083 to your computer and use it in GitHub Desktop.
Execute mysql query and write to file utilizing a streaming cursor
#! /usr/bin/env python
from sys import argv
import csv
import MySQLdb
from MySQLdb import cursors
mysql_params = [argv[1], argv[2], argv[3], argv[4], argv[5]]
output_file = argv[6]
mysql_query = argv[7]
# MySQL connection initialization
try:
mcon = MySQLdb.connect(host=params[0], port=params[1], user=params[2], passwd=params[3], db=params[4], use_unicode=True, cursorclass = MySQLdb.cursors.SSCursor)
mcur = mcon.cursor()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
mcur.execute("SET NAMES 'utf8';")
mcur.execute("SET @@NET_WRITE_TIMEOUT = 900;")
return mcur, mcon
# Write MySQL result set to CSV line by line
print "[!] Executing MySQL query"
mcur.execute(mysql_query)
file = open(output_file, 'w')
writer = csv.writer(file, delimiter = delim, quotechar = '"', quoting = csv.QUOTE_MINIMAL)
print "[!] Writing result set to file '"+output_file+"'"
while True:
# Get one record from result set
row = mcur.fetchone()
# If no records left, break loop
if not row:
break
# Write record to file in comma-delimited format
writer.writerow(row)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment