24

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?

tijko
  • 7,599
  • 11
  • 44
  • 64

10 Answers10

33

From the sqlite FAQ:

From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python bindings) you can get access to table and index names by doing a SELECT on a special table named "SQLITE_MASTER". Every SQLite database has an SQLITE_MASTER table that defines the schema for the database. The SQLITE_MASTER table looks like this:

CREATE TABLE sqlite_master (
  type TEXT,
  name TEXT,
  tbl_name TEXT,
  rootpage INTEGER,
  sql TEXT
);

So to get a list of all table names execute:

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;

To get column names for a given table, use the pragma table_info command:

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column.

This command works just fine from python:

>>> import sqlite3
>>> conn = sqlite3.connect(':mem:')
>>> for row in conn.execute("pragma table_info('sqlite_master')").fetchall():
...     print row
... 
(0, u'type', u'text', 0, None, 0)
(1, u'name', u'text', 0, None, 0)
(2, u'tbl_name', u'text', 0, None, 0)
(3, u'rootpage', u'integer', 0, None, 0)
(4, u'sql', u'text', 0, None, 0)

Unfortunately pragma statements do not work with parameters; you'll have to manually insert the table name (make sure it's not sourced from an untrusted source and escape it properly).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Awesome answer, thanks. The first part I understand but, the pragma_info I don't. I have much to learn. I would of thought that the fields would be shown like in Paul McNetts answer with the `.description` method, below? – tijko Aug 16 '12 at 23:07
  • 1
    @tijko: The `pragma` sql statement returns rows just like a select statement, so view it like a `select * from table_info where tablename = 'sqlite_master'` if that helps. :-) – Martijn Pieters Aug 16 '12 at 23:14
  • 1
    @tijko: The `.description` trick returns slightly different info, see [PEP 249](http://www.python.org/dev/peps/pep-0249/) for the details. I feel that if you want to introspect a SQLite database, then using `pragma table_info` is more direct, and `.description` gives you column info for the executed query, not necessarily all columns in the table you queried (e.g. `select columna, columnb, 1 from table;` gives you 3 entries in `cursor.description`). – Martijn Pieters Aug 16 '12 at 23:14
  • @Martijin Pieters yes, this is very helpful. Should have tried that part above with a specific table after finding them. Thanks for the help! – tijko Aug 17 '12 at 00:06
  • I agree with @MartijnPieters on this one, by the way. I just wanted to show another way to get the info with one fewer database query, plus show the python code. – Paul McNett Aug 17 '12 at 00:29
27

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;
Tom Kerr
  • 10,444
  • 2
  • 30
  • 46
  • The first part works great, thanks! I'm still not sure how I would find the fields for each table. I can query the table and find whats inside but, not the actual fieldname? – tijko Aug 16 '12 at 22:47
  • @tijko Looks like Martjin's answer has python specific info about how to get the column names. I upvoted his answer! – Tom Kerr Aug 16 '12 at 22:53
  • 3
    Just a note, the second query won't work if no rows have been added to the table yet. – Michael Apr 25 '16 at 01:04
  • you might want to include indexes: `select sql from sqlite_master where type in ('table','index');` – Alec Istomin Apr 11 '20 at 04:37
9

Here's a convenient printer I wrote based Martijn's response:

def printSchema(connection):
    for (tableName,) in connection.execute(
        """
        select NAME from SQLITE_MASTER where TYPE='table' order by NAME;
        """
    ):
        print("{}:".format(tableName))
        for (
            columnID, columnName, columnType,
            columnNotNull, columnDefault, columnPK,
        ) in connection.execute("pragma table_info('{}');".format(tableName)):
            print("  {id}: {name}({type}){null}{default}{pk}".format(
                id=columnID,
                name=columnName,
                type=columnType,
                null=" not null" if columnNotNull else "",
                default=" [{}]".format(columnDefault) if columnDefault else "",
                pk=" *{}".format(columnPK) if columnPK else "",
            ))
2

To get the field names, use cur.description after the query:

import sqlite3.dbapi2 as sqlite
con = sqlite.connect(":memory:")
cur = con.cursor()
con.executescript("""
    create table test (name, address);
    insert into test (name, address) values ("Jer", "Monterey Street");
""")

cur.execute("select * from test where 1=0")
rs = cur.fetchall()  ## will be [] because of where clause
field_names = [r[0] for r in cur.description]
Paul McNett
  • 847
  • 6
  • 9
  • Hi Paul, thanx for that. I am just wondering what is the line... rs = cur.fetchall() for rs is not referenced anywhere else – joe_evans Jul 10 '20 at 13:44
1

Use sqlite row object. A row object has keys() that will give you the schema.

from docs.python.org

conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('select * from stocks')
  <sqlite3.Cursor object at 0x7f4e7dd8fa80>
r = c.fetchone()
type(r)
  <type 'sqlite3.Row'>
  r
  (u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14)
r.keys()
  ['date', 'trans', 'symbol', 'qty', 'price']
Perennial
  • 438
  • 3
  • 8
1

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.

starturtle
  • 699
  • 8
  • 25
1

To get the schema information, IMHO, below also works:

select sql from sqlite_master where type='table';
Vimanyu
  • 625
  • 2
  • 9
  • 24
1

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)
DrBwts
  • 3,470
  • 6
  • 38
  • 62
Mitul
  • 11
  • 1
0

result sets have a description that you can get some information from. It reveals some basic metadata like column name and number of columns.

>>> rs = c.execute('''SELECT * FROM news WHERE 1=0''');
>>> dir(rs)
['__class__', '__delattr__', '__doc__', '__format__',  
'__getattribute__', '__hash__', '__init__', '__iter__', '__new__',  
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'arraysize', 'close', 'connection',
**'description'**, 'execute', 'executemany', 'executescript', 'fetchall',
'fetchmany', 'fetchone', 'lastrowid', 'next', 'row_factory',
'rowcount', 'setinputsizes', 'setoutputsize']



>>> print(rs.description)
(('id', None, None, None, None, None, None), 
('imageUrl', None, None, None, None, None, None), 
('headline', None, None, None, None, None, None), 
('who', None, None, None, None, None, None))
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0

Assuming the name of the database is my_db and the name of the table is my_table, to get the name of the columns and the datatypes:

con = sqlite.connect(my_db)
cur = con.cursor()
query =  "pragma table_info({})".format(my_table)
table_info = cur.execute(query).fetchall()

It returns a list of tuples. Each tuple has the order, name of the column and data type.

afar
  • 135
  • 2
  • 6