pt

Pretty Table support. Allow you quickly print query result in ascii table.

from_everything()

中文文档

因为 PrettyTable 只能通过 from_db_cursor 来创建, 也就是说要求返回 ResultProxy, 而不是 ORM 对象的列表. 所以在 from_object(), from_query() 这些使用 session.query(Entity) 的函数中, 我们需要用特殊技巧, 让 session.query 最终返回 ResultProxy.

sqlalchemy_mate.pt.from_stmt(stmt, engine, **kwargs)[source]

Execute a query in form of texture clause, return the result in form of

PrettyTable.

Parameters:
  • stmt (TextClause) –
  • engine (Engine) –
Return type:

PrettyTable

sqlalchemy_mate.pt.from_sql(sql, engine, limit=None, **kwargs)[source]

Create a prettytable.PrettyTable from sqlalchemy.select.

Parameters:
  • sql (Select) – a sqlalchemy.sql.selectable.Select object.
  • engine (Engine) – an sqlalchemy.engine.base.Engine object.
  • limit (int) – int, limit rows to return.
Return type:

PrettyTable

中文文档

将sqlalchemy的sql expression query结果放入prettytable中.

Note

注意, from_db_cursor是从原生的数据库游标通过调用fetchall()方法来获取数据。 而sqlalchemy返回的是ResultProxy类。所以我们需要从中获取游标 至于为什么不能直接使用 from_db_cursor(engine.execute(sql).cursor) 的语法 我也不知道为什么.

sqlalchemy_mate.pt.from_query(query, engine_or_session=None, limit=None, **kwargs)[source]

Execute an ORM style query, and return the result in prettytable.PrettyTable.

Parameters:
  • query (Query) – an sqlalchemy.orm.Query object.
  • engine (Any) – query is always associated with a session, this parameter just reserve the arg place.
  • limit (int) – int, limit rows to return.
Return type:

PrettyTable

Returns:

a prettytable.PrettyTable object

中文文档

将通过 ORM 的查询结果中的数据放入 PrettyTable 中.

sqlalchemy_mate.pt.from_table(table, engine, limit=None, **kwargs)[source]

Select data in a database table and put into prettytable.

Create a prettytable.PrettyTable from sqlalchemy.Table.

Parameters:
  • table (Table) – a sqlalchemy.sql.schema.Table object
  • engine (Engine) – the engine or session used to execute the query.
  • limit (int) – int, limit rows to return.
Return type:

PrettyTable

中文文档

将数据表中的数据放入 PrettyTable 中.

sqlalchemy_mate.pt.from_object(orm_class, engine_or_session, limit=None, **kwargs)[source]

Select data from the table defined by a ORM class, and put into prettytable

Parameters:
  • orm_class – an orm class inherit from sqlalchemy.ext.declarative.declarative_base()
  • engine_or_session (Union[Engine, Session]) – the engine or session used to execute the query.
  • limit (int) – int, limit rows to return.
Return type:

PrettyTable

中文文档

将数据对象的数据放入 PrettyTable 中.

常用于快速预览某个对象背后的数据.

sqlalchemy_mate.pt.from_resultproxy(result_proxy, **kwargs)[source]

Construct a Prettytable from ResultProxy.

Parameters:result_proxy – a sqlalchemy.engine.result.ResultProxy object.
sqlalchemy_mate.pt.from_data(data)[source]

Construct a Prettytable from list of rows.

Return type:PrettyTable
sqlalchemy_mate.pt.from_everything(everything, engine_or_session, limit=None, **kwargs)[source]

Construct a Prettytable from any kinds of sqlalchemy query.

Return type:PrettyTable

Usage:

from sqlalchemy import select

sql = select([t_user])
print(from_everything(sql, engine))

query = session.query(User)
print(from_everything(query, session))

session.query(User)