简体   繁体   English

Sqlalchemy:打印表的内容

[英]Sqlalchemy: Print contents of table

I want to print all the contents of a table using sqalchemy.我想使用 sqalchemy 打印表格的所有内容。 I can't seem to figure out how inspect works.我似乎无法弄清楚检查是如何工作的。

I want to achieve something like that:我想实现这样的目标:

column1: value1, column2: value4
column1: value2, column2: value5
column1: value3, column2: value6

In a table that looks like this:在看起来像这样的表中:

Table_1:
+---------+---------+
| column1 | column2 |
+---------+---------+
| value1  | value4  |
| value2  | value5  |
| value3  | value6  |
+---------+---------+

While I don't know how to do this with inspect , I achieve the desire output through regular queries.虽然我不知道如何使用inspect做到这一点,但我通过常规查询实现了期望的输出。 For this example, I created a sqlite table based on your example.对于此示例,我根据您的示例创建了一个sqlite表。 First, we connect and reflect on this existing database.首先,我们连接并反思这个现有的数据库。

from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
eng = create_engine("sqlite:///databases/example_table.db")

Base = automap_base()
Base.prepare(eng, reflect=True)
Table = Base.classes.example_table

To facilitate our query, we instantiate a session ,为了方便我们的查询,我们实例化一个session

Session = sessionmaker(bind=eng)
session = Session()

and perform the query, saving the outcome to result .并执行查询,将结果保存到result

stmt = select('*').select_from(Table)
result = session.execute(stmt).fetchall()

The elements of this query are instances of the sqlalchemy RowProxy class, which has a keys method that can be used to access the column names.这个查询的元素是实例sqlalchemy RowProxy类,它有一个keys的方法可被用于访问的列名。 Consequently, we can transform the result with a few short functions.因此,我们可以使用一些简短的函数来转换result

def result_dict(r):
    return dict(zip(r.keys(), r))

def result_dicts(rs): 
    return list(map(result_dict, rs))

result_dicts(result)

which returns返回

[{'id': 1, 'column1': 'value1', 'column2': 'value4'},
 {'id': 2, 'column1': 'value2', 'column2': 'value5'},
 {'id': 3, 'column1': 'value3', 'column2': 'value6'}]

Maybe you have a look here:也许你看看这里:

http://docs.sqlalchemy.org/en/rel_1_0/core/reflection.html http://docs.sqlalchemy.org/en/rel_1_0/core/reflection.html

The inspector object could help you solving your problem.检查器对象可以帮助您解决问题。

I don't know how useful this might be, but you can visualize the table in desperate times or you need a quick sneak peek at the table.我不知道这可能有多大用处,但是您可以在绝望的时候想象表格,或者您需要快速偷看表格。

# create an engine using the following code and 
# replace it with the path to your .db file.

from sqlalchemy import create_engine
engine = create_engine('sqlite:///employee.db', echo = False)

# Import pandas and connect the engine
# use the lowercase representation of your table name for table_name parameter 
# For ex:
class Users(db.Model):
    ...
    ...
    ...

import pandas as pd

user_table = pd.read_sql_table(table_name="users", con=engine)
# This will load the table as dataframe and then you can display

I understand that in case the database is huge, visualizing it using pandas may not be the best idea but as I said above, desperate times !我知道如果数据库很大,使用 Pandas 对其进行可视化可能不是最好的主意,但正如我上面所说,绝望的时刻!

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

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