title: In Python, display output of SQL query as a table, just like it does in SQL
body: <p>this seems like a basic function, but I'm new to Python so maybe I'm not googling this correctly.</p>
  
  <p>In Microsoft SQL Server, when you have a statement like</p>
  
  <pre><code>SELECT top 100 * FROM dbo.Patient_eligibility
  </code></pre>
  
  <p>you get a result like </p>
  
  <pre><code> Patient_ID | Patient_Name | Patient_Eligibility
  
       67456  | Smith, John  | Eligible
       ...         
       etc.
  </code></pre>
  
  <p>Etc.</p>
  
  <p>I am running a connection to SQL through Python as such, and would like the output to look exactly the same as in SQL. Specifically - with column names and all the data rows specified in the SQL query. It doesn't have to appear in the console or the log, I just need a way to access it to see what's in it.</p>
  
  <p>Here is my current code attempts:</p>
  
  <pre><code>import pyodbc
  conn = pyodbc.connect(connstr)
  cursor = conn.cursor()
  
  sql = "SELECT top 100 * FROM [dbo].[PATIENT_ELIGIBILITY]"
  cursor.execute(sql)
  data = cursor.fetchall()
  
  #Query1
  for row in data :
      print (row[1])
  
  #Query2
  print (data)
  
  #Query3
  data
  </code></pre>
  
  <p>My understanding is that somehow the results of PATIENT_ELIGIBILITY are stored in the variable data. Query 1, 2, and 3 represent methods of accessing that data that I've googled for - again seems like basic stuff.</p>
  
  <p>The results of #Query1 give me the list of the first column, without a column name in the console. In the variable explorer, 'data' appears as type List. When I open it up, it just says 'Row object of pyodbc module' 100 times, one for each row. Not what I'm looking for. Again, I'm looking for the same kind of view output I would get if I ran it in Microsoft SQL Server.</p>
  
  <p>Running #Query2 gets me a little closer to this end. The results appear like a .csv file - unreadable, but it's all there, in the console.</p>
  
  <p>Running #Query3, just the 'data' variable, gets me the closest result but with no column names. How can I bring in the column names?</p>
  
  <p>More directly, how do i get 'data' to appear as a clean table with column names somewhere? Since this appears a basic SQL function, could you direct me to a SQL-friendly library to use instead?</p>
  
  <p>Also note that neither of the Queries required me to know the column names or widths. My entire method here is attempting to eyeball the results of the Query and quickly check the data - I can't see that the Patient_IDs are loading properly if I don't know which column is patient_ids.</p>
  
  <p>Thanks for your help!</p>