Is there a way to get a schema of a database from within python?

前端 未结 10 1936
走了就别回头了
走了就别回头了 2020-12-09 16:06

I\'m trying to find out a way to find the names of tables in a database(if any exist). I find that from a sqlite cli I can use:

>.tables
<
10条回答
  •  庸人自扰
    2020-12-09 17:08

    make the connection to the database

    connection = connect_db('./database_name.db')
    

    print the table names

    table_names = [t[0] for t in connection.execute("SELECT name FROM sqlite_master WHERE type='table';")]
    print(table_names)
    

提交回复
热议问题