title: Import ms sql database data in excel file using python
body: <p>I am trying to import ms sql database data in excel file using python. I am not data getting in proper order in excel sheet. e.g it is showing column 1 data then column 3 then column 2 and then column 4 and so on.
  I am using the following script:</p>
  
  <pre><code>import pyodbc
  import csv
  connection = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                    "Server=MS-DC-06,49208;"
                    "Database=****;"
                    "uid=**;pwd=***")
  cursor = connection.cursor()
  cursor.execute("select top 10 * from Api_NotificationEntry")
  data=cursor.fetchall()
  for col in data:
      with open('dataTester.csv', 'a', newline='') as fp:
        a= csv.writer(fp, delimiter=',')
        a.writerow({col[0],col[1]})
  cursor.close()
  connection.close()
  </code></pre>
  
  <p>It should show data like this:</p>
  
  <pre><code>10 2689 
  11 2689 
  12 2689 
  13 2689 
  14 2689 
  15 2689 
  16 2689 
  8 2673 
  17 2689 
  19 3083 
  </code></pre>
  
  <p>But it is showing like below in excel sheet:</p>
  
  <pre><code>Col1    Col2
  65509   
  10  
  2689    
  65509   
  10  
  2689    
  65509   
  2689    10
  2689    65509
  6218    65509
  2689    10
  2689    65509
  6218    65509
  2689    10
  2689    11
  2689    12
  2689    13
  2689    14
  2689    15
  16  2689
  8   2673
  17  2689
  3083    19
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/yWsn3.png" rel="nofollow noreferrer">This is how it looks like in excel screenshot</a></p>
  
  <p><a href="https://i.stack.imgur.com/lyk81.png" rel="nofollow noreferrer">This is how it looks like in notepad</a></p>