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

database-schema, python, sqlite

Solution

You should be able access the table names from the `sqlite_master` table.

SELECT name FROM sqlite_master WHERE type='table';

The names of the columns are not directly accessible. The easiest way to get them is to query the table and get the column names from the query result.

SELECT * FROM table_name LIMIT 1;

Problem

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 ``` Then for the fields: ``` >PRAGMA TABLE_INFO(table_name) ``` This obviously doesn't work within python. Is there even a way to do this with python or should I just be using the sqlite command-line?

Original source