EXPORT FROM SQL SERVER TO CSV - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: EXPORT FROM SQL SERVER TO CSV (/thread-9586.html) |
EXPORT FROM SQL SERVER TO CSV - PYTHONDUDE - Apr-17-2018 The below code works however, whenever it gets exported, the headers for each column is missing. I need it to include all headers please advise import pyodbc import csv conn =("Driver={SQLSERVER Native Client 11.0};" "Server = blah" "database = mydb;" cursor = coon.cursor() sql = """Select top 5 * from mytable""" cursor.execute(sql) row = cursor.fetchall() with open('test.csv', 'w', newline= '') as f: a = csv.writer(f, delimiter=',') a.writerows(rows cursor.close() RE: EXPORT FROM SQL SERVER TO CSV - woooee - Apr-17-2018 You write that row separately with open('test.csv', 'w', newline= '') as f: a = csv.writer(f, delimiter=',') a.writerow(["Header 1", "Header 2"]) ## etc a.writerows(rows) ## closing paren added RE: EXPORT FROM SQL SERVER TO CSV - PYTHONDUDE - Apr-17-2018 thank you that worked |