List database tables with SQLAlchemy

前端 未结 4 1283
心在旅途
心在旅途 2020-12-13 01:52

I want to implement a function that gives information about all the tables (and their column names) that are present in a database (not only those created with SQLAlchemy).

4条回答
  •  醉酒成梦
    2020-12-13 02:29

    Hey I created a small module that helps easily reflecting all tables in a database you connect to with SQLAlchemy, give it a look: EZAlchemy

    from EZAlchemy.ezalchemy import EZAlchemy
    
    DB = EZAlchemy(
        db_user='username',
        db_password='pezzword',
        db_hostname='127.0.0.1',
        db_database='mydatabase',
        d_n_d='mysql'   # stands for dialect+driver
    )
    
    # this function loads all tables in the database to the class instance DB
    DB.connect()
    
    # List all associations to DB, you will see all the tables in that database
    dir(DB)
    

提交回复
热议问题