Created
September 26, 2013 16:59
-
-
Save erincerys/6717083 to your computer and use it in GitHub Desktop.
Execute mysql query and write to file utilizing a streaming cursor
This file contains 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
#! /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