0

this seems like a basic function, but I'm new to Python so maybe I'm not googling this correctly.

In Microsoft SQL Server, when you have a statement like

SELECT top 100 * FROM dbo.Patient_eligibility

you get a result like

 Patient_ID | Patient_Name | Patient_Eligibility

     67456  | Smith, John  | Eligible
     ...         
     etc.

Etc.

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.

Here is my current code attempts:

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

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.

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.

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.

Running #Query3, just the 'data' variable, gets me the closest result but with no column names. How can I bring in the column names?

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?

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.

Thanks for your help!

Samsonite Manly
  • 363
  • 2
  • 5
  • 13
  • Possible duplicate of [Execute SQL from Python: Format the result set](https://stackoverflow.com/questions/43504474/execute-sql-from-python-format-the-result-set) – ivan_pozdeev Jun 14 '18 at 17:16
  • I personally had better luck with my python/mssql work with pypyodbc, might be worth looking at. Not exactly what you are looking todo, but might be enough: https://stackoverflow.com/questions/12704305/return-column-names-from-pyodbc-execute-statement – sniperd Jun 14 '18 at 17:24

1 Answers1

1

It's more than 1 question, I'll try help you and give advice.

  1. I am running a connection to SQL through Python as such, and would like the output to look exactly the same as in SQL.

You are mixing SQL as language and formatted output of some interactive SQL tool. SQL itself does not have anything about "look" of data.

  1. 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.

Correct. cursor.fetchall returns only data.

Field informations can be read from cursor.description.

Read more in PEP-O249

  1. how do i get 'data' to appear as a clean table with column names somewhere?

It depends how do you define "appear".

You want: text output, html page or maybe GUI?

  • For text output: you can read column names from cursor.description and print them before data.

  • If you want html/excel/pdf/other - find some library/framework suiting your taste.

  • If you want an interactive experience similar to SQL tools - I recommend to look on jupyter-notebook + pandas.

Something like:

pandas.read_sql_query(sql)

will give you "clean table" nothing worse than SQLDeveloper/SSMS/DBeaver/other gives.

Alex Yu
  • 2,366
  • 1
  • 20
  • 30
  • Thanks. I found the solution in Spyder environment. In the variable explorer, I can double click the variable name into which i store the data output, and a little tabulated chart comes up like in Microsoft SQL Server or SAS. Aside from that, I wonder if there is a quick function to juts copy the entire output to the clipboard, while keeping the formatting/delimeters when pasting into EXcel. – Samsonite Manly Jun 15 '18 at 20:55