简体   繁体   English

在Python中,将SQL查询的输出显示为表格,就像在SQL中一样

[英]In Python, display output of SQL query as a table, just like it does in SQL

this seems like a basic function, but I'm new to Python so maybe I'm not googling this correctly. 这似乎是一个基本功能,但是我是Python的新手,所以也许我没有正确搜索它。

In Microsoft SQL Server, when you have a statement like 在Microsoft SQL Server中,当您有如下语句时

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. 我正在通过Python运行与SQL的连接,并且希望输出看起来与SQL中的输出完全相同。 Specifically - with column names and all the data rows specified in the SQL query. 具体来说-具有列名和SQL查询中指定的所有数据行。 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. 我的理解是,以某种方式将PATIENT_ELIGIBILITY的结果存储在变量数据中。 Query 1, 2, and 3 represent methods of accessing that data that I've googled for - again seems like basic stuff. 查询1、2和3表示访问我用Google搜索过的数据的方法-再次看起来像是基本的东西。

The results of #Query1 give me the list of the first column, without a column name in the console. #Query1的结果为我提供了第一列的列表,但在控制台中没有列名。 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. 当我打开它时,它只会说“ pyodbc模块的行对象” 100次,每行一次。 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. 同样,我正在寻找与Microsoft SQL Server中运行的视图输出相同的视图输出。

Running #Query2 gets me a little closer to this end. 运行#Query2使我更接近此目的。 The results appear like a .csv file - unreadable, but it's all there, in the console. 结果看起来像一个.csv文件-不可读,但就在控制台中。

Running #Query3, just the 'data' variable, gets me the closest result but with no column names. 运行#Query3(仅是'data'变量)可以得到最接近的结果,但没有列名。 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? 由于这似乎是基本的SQL函数,您能否将我引导到一个SQL友好的库中代替使用?

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. 我在这里的整个方法是尝试查看查询结果并快速检查数据-如果我不知道哪一列是Patient_id,则看不到Patient_ID正在正确加载。

Thanks for your help! 谢谢你的帮助!

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. 我正在通过Python运行与SQL的连接,并且希望输出看起来与SQL中的输出完全相同。

You are mixing SQL as language and formatted output of some interactive SQL tool. 您正在将SQL作为语言和某种交互式SQL工具的格式化输出进行混合。 SQL itself does not have anything about "look" of data. SQL本身与数据的“外观”无关。

  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. 我在这里的整个方法是尝试查看查询结果并快速检查数据-如果我不知道哪一列是Patient_id,则看不到Patient_ID正在正确加载。

Correct. 正确。 cursor.fetchall returns only data. cursor.fetchall仅返回数据。

Field informations can be read from cursor.description . 字段信息可以从cursor.description读取。

Read more in PEP-O249 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? 您想要:文本输出,html页面还是GUI?

  • For text output: you can read column names from cursor.description and print them before data. 对于文本输出:您可以从cursor.description读取列名称,并在数据之前打印它们。

  • If you want html/excel/pdf/other - find some library/framework suiting your taste. 如果您想要html / excel / pdf / other-找到一些适合您口味的库/框架。

  • If you want an interactive experience similar to SQL tools - I recommend to look on jupyter-notebook + pandas . 如果您想获得类似于SQL工具的交互体验-我建议您使用jupyter-notebook + pandas

Something like: 就像是:

pandas.read_sql_query(sql)

will give you "clean table" nothing worse than SQLDeveloper/SSMS/DBeaver/other gives. 会给您“干净的表”,这比SQLDeveloper / SSMS / DBeaver / other所提供的还差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM