How to print all columns in SQLAlchemy ORM

后端 未结 10 1969
再見小時候
再見小時候 2020-12-28 15:18

Using SQLAlchemy, I am trying to print out all of the attributes of each model that I have in a manner similar to:

SELECT * from table;

How

10条回答
  •  孤城傲影
    2020-12-28 15:30

    I'm using SQL Alchemy v 1.0.14 on Python 3.5.2

    Assuming you can connect to an engine with create_engine(), I was able to display all columns using the following code. Replace "my connection string" and "my table name" with the appropriate values.

    from sqlalchemy import create_engine, MetaData, Table, select
    
    engine = create_engine('my connection string')
    
    conn = engine.connect()
    metadata = MetaData(conn)
    t = Table("my table name", metadata, autoload=True)
    columns = [m.key for m in t.columns]
    columns
    

    the last row just displays the column names from the previous statement.

提交回复
热议问题