Python Forum
Output SQL to csv or xls file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output SQL to csv or xls file?
#1
Hi,
Have a bit of code that runs a sql query - can't seem to get it to output to a .csv or an .xlsx file. Any help is appreciated

The window has several buttons, each of which should generate a report. The button label in this example is "Count of All Codes" - just trying to change the last line so that it writes to a file like "C:/mytestfile.csv"

if event =="Count of All Codes":
    connstring = 'DRIVER={SQL Server};SERVER=XXX;DATABASE=XXX;UID=XXX;PWD=XXX'
    SQLStr = "SELECT subsource_type_desc,  COUNT(*) AS 'num' FROM [TSCACS] GROUP BY subsource_type_desc order by num desc"
    conn = pyodbc.connect(connstring)
    cursor = conn.cursor()
    cursor.execute(SQLStr)
    for row in cursor:
        print(row)
Thanks again for any help.
Reply


Messages In This Thread
Output SQL to csv or xls file? - by JP_ROMANO - Aug-01-2019, 11:13 PM
RE: Output SQL to csv or xls file? - by ichabod801 - Aug-02-2019, 12:46 AM
RE: Output SQL to csv or xls file? - by JP_ROMANO - Aug-02-2019, 01:33 AM
RE: Output SQL to csv or xls file? - by ichabod801 - Aug-02-2019, 01:46 AM
RE: Output SQL to csv or xls file? - by JP_ROMANO - Aug-02-2019, 01:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,215 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Output CSV file with filepath and file contents glittergirl 1 1,805 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  csv file output rturus 7 3,414 Jan-30-2020, 02:09 PM
Last Post: buran
  File name as part of output file name Jeeping_Coder 1 2,180 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 2,112 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 3,788 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020