Python csv from database query adding a custom column to csv file
Question: here is what I try to achieve my current code is working fine I get the query
to run on my sql server but I will need to gather information from several
servers. How would I add a column with the dbserver listed in that column?

    
    
    import pyodbc
    import csv
    
    f = open("dblist.ini")
    dbserver,UID,PWD = [ variable[variable.find("=")+1 :] for variable in f.readline().split("~")]
    
    connectstring = "DRIVER={SQL server};SERVER=" + dbserver + ";DATABASE=master;UID="+UID+";PWD="+PWD
    
    cnxn = pyodbc.connect(connectstring)
    cursor = cnxn.cursor()
    
    fd = open('mssql1.txt', 'r')
    sqlFile = fd.read()
    fd.close()
    
    cursor.execute(sqlFile)
    
    
    with open("out.csv", "wb") as csv_file:
        csv_writer = csv.writer(csv_file, delimiter = '!')
        csv_writer.writerow([i[0] for i in cursor.description]) # write headers
        csv_writer.writerows(cursor)
    


Answer: You could add the extra information in your sql query. For example:

    
    
    select "dbServerName", * from table;
    

Your cursor will return with an extra column in front of your real data that
has the db Server name. The downside to this method is you're transferring a
little more extra data.

