python查询数据库所有表名和字段,使用SQLAlchemy查询所有表名

来源:互联网 发布:义阳博弈量能 知乎 编辑:程序博客网 时间:2024/05/01 21:51

 方法1:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()        engine = create_engine('sqlite:///path',echo=True)        Base.metadata.reflect(engine)        tables = Base.metadata.tables        print(tables)
  • 12345

tables是一个immutabledict,格式如下:

immutabledict({'users': Table('users', MetaData(bind=None), Column('id', INTEGER(), table=<users>, primary_key=True, nullable=False), Column('name', VARCHAR(), table=<users>), Column('fullname', VARCHAR(), table=<users>), Column('password', VARCHAR(), table=<users>), schema=None)})
  • 1

经测试这个immutabledict没有has_key()方法,但是可以用if ‘users’ in tables.keys()判断

此方法打印出来的包括表名、字段名、字段类型、字符格式

官方文档在这里

方法2:

打印 engine.table_names() 时它列出的所有表。

仅有表名

阅读全文
0 0
原创粉丝点击