Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Created August 13, 2018 13:20
Show Gist options
  • Select an option

  • Save wesleyit/2701d166cda2d926a9bcccbd9e01a1df to your computer and use it in GitHub Desktop.

Select an option

Save wesleyit/2701d166cda2d926a9bcccbd9e01a1df to your computer and use it in GitHub Desktop.
Get a CSV file from a SQL query using batches of 1000 lines
import sys
import pyodbc as odbc
import datetime
dsn = sys.argv[1]
query_file = sys.argv[2]
csv_file = sys.argv[3]
print(f'Connecting to {dsn}')
print(f'Executing {query_file}')
print(f'Saving to {csv_file}\n')
conn = odbc.connect(dsn)
cursor = conn.cursor()
cursor.execute(open(query_file).read())
print('Query executed. Fetching results...\n')
if sys.argv[4] == '--debug': print(cursor.description)
counter = 0
with open(csv_file, 'w') as f:
columns = list(zip(*cursor.description))[0]
c_size = len(columns)
data = {}
for i, v in enumerate(columns):
f.write(v)
if i < c_size - 1: f.write(';')
f.write('\n')
while True:
rows = cursor.fetchmany(1000)
for row in rows:
counter += 1
if not row:
break
else:
for i, v in enumerate(columns):
reg = row[i]
if type(reg) == datetime.datetime: reg = reg.isoformat()
data[v] = reg
f.write(str(reg))
if i < c_size - 1: f.write(';')
f.write('\n')
if counter % 5000 == 0:
sys.stdout.write(f'\rProcessing line {counter} of {cursor.rowcount}.')
print('\nDone!\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment