Using Windows 10/PyCharm/Python 3.6 - am able to click a button on my UI (PySimpleGui), connect to sql and run my query. I can get the results to pour into a .csv file - but I'm not able to get the headers. Any help is appreciated! The button is labeled "GET ALL DATA"
Have tried massaging content from here, in various ways with numerous modifications to the syntax and structure - to no avail:
https://www.oreilly.com/library/view/python-cookbook/0596001673/ch08s11.html
https://kadler.github.io/2018/01/08/fetching-python-database-cursors-by-column-name.html#
Here's a sample of one button's click event
if event =="GET ALL DATA":
connstring = 'DRIVER={SQL Server};SERVER=xxx;DATABASE=xxx;UID=xxx;PWD=xxx'
SQLStr = "SELECT * FROM [TSCACS]"
conn = pyodbc.connect(connstring)
cursor = conn.cursor()
cursor.execute(SQLStr)
with open("F:\TOOLS\Button1Output.csv", 'w',encoding='utf-8') as myfile:
mywriter = csv.writer(myfile, delimiter=',', lineterminator='\r')
for row in cursor:
mywriter.writerow(row)
Nearly every time I run it I get a new error message or simply no headers. The single table I'm querying currently has 56 columns and will likely grow to about 78, so I'd rather not have to hard code them anywhere!
Thank you for any assistance
cursor.description. Probably this should give names[d[0] for d in cursor.description]headers = [d[0] for d in cursor.description],mywriter.writerow(headers).