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

前端 未结 10 1935
走了就别回头了
走了就别回头了 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:01

    I just tried

    SELECT name FROM my_db.sqlite_master WHERE type='table';
    

    to combine Tom Kerr's answer and the attempt to retrieve information on an attached database. At first it didn't work. Turns out I first have to attach the other database this way:

    ATTACH DATABASE 'file:my_other_database_file.db?cache=shared' as my_db;
    

    otherwise the database will fail to obtain a read lock for the attached database's sqlite_master (and all queries will succeed with zero results). Just a hint in case anybody else stumbles upon that part of the issue.

提交回复
热议问题