Skip to main content

A Python library to dump table schema of a SQLite database file.

Project description

sqliteschema

https://badge.fury.io/py/sqliteschema.svg https://img.shields.io/pypi/pyversions/sqliteschema.svg https://img.shields.io/travis/thombashi/sqliteschema/master.svg?label=Linux https://img.shields.io/appveyor/ci/thombashi/sqliteschema/master.svg?label=Windows https://coveralls.io/repos/github/thombashi/sqliteschema/badge.svg?branch=master

Summary

A Python library to dump table schema of a SQLite database file.

Installation

pip install sqliteschema

Usage

Full example can be found at examples/get_table_schema.py

Extract SQLite Schema

Sample Code:
for verbosity_level in range(2):
    print("----- get_table_schema method: verbosity_level={} -----".format(
        verbosity_level))
    extractor = sqliteschema.SqliteSchemaExtractor(
        sqlite_db_path, verbosity_level=verbosity_level,
        output_format="table")
    for table_name in extractor.get_table_name_list():
        print("{:s} {}".format(
            table_name,
            extractor.get_table_schema(table_name)))
    print()
Output:
----- get_table_schema method: verbosity_level=0 -----
sampletable0 ['attr_a', 'attr_b']
sampletable1 ['foo', 'bar', 'hoge']
constraints ['primarykey_id', 'notnull_value', 'unique_value']

----- get_table_schema method: verbosity_level=1 -----
sampletable0 OrderedDict([('attr_a', 'INTEGER'), ('attr_b', 'INTEGER')])
sampletable1 OrderedDict([('foo', 'INTEGER'), ('bar', 'REAL'), ('hoge', 'TEXT')])
constraints OrderedDict([('primarykey_id', 'INTEGER'), ('notnull_value', 'REAL'), ('unique_value', 'INTEGER')])

Dump SQLite Schema as Table Text

Sample Code:
for verbosity_level in range(2):
    print("----- dump schema table: verbosity_level={} -----".format(
        verbosity_level))
    extractor = sqliteschema.SqliteSchemaExtractor(
        sqlite_db_path, verbosity_level=verbosity_level,
        output_format="table")
    print(extractor.dumps())
Output:
----- dump schema table: verbosity_level=0 -----
.. table:: sampletable0

    +--------------+---------+
    |Attribute name|Data type|
    +==============+=========+
    |attr_a        |INTEGER  |
    +--------------+---------+
    |attr_b        |INTEGER  |
    +--------------+---------+

.. table:: sampletable1

    +--------------+---------+
    |Attribute name|Data type|
    +==============+=========+
    |foo           |INTEGER  |
    +--------------+---------+
    |bar           |REAL     |
    +--------------+---------+
    |hoge          |TEXT     |
    +--------------+---------+

.. table:: constraints

    +--------------+---------+
    |Attribute name|Data type|
    +==============+=========+
    |primarykey_id |INTEGER  |
    +--------------+---------+
    |notnull_value |REAL     |
    +--------------+---------+
    |unique_value  |INTEGER  |
    +--------------+---------+


----- dump schema table: verbosity_level=1 -----
.. table:: sampletable0 (2 records)

    +--------------+---------+-----------+--------+------+-----+
    |Attribute name|Data type|Primary key|Not NULL|Unique|Index|
    +==============+=========+===========+========+======+=====+
    |attr_a        |INTEGER  |           |        |      |     |
    +--------------+---------+-----------+--------+------+-----+
    |attr_b        |INTEGER  |           |        |      |     |
    +--------------+---------+-----------+--------+------+-----+

.. table:: sampletable1 (2 records)

    +--------------+---------+-----------+--------+------+-----+
    |Attribute name|Data type|Primary key|Not NULL|Unique|Index|
    +==============+=========+===========+========+======+=====+
    |foo           |INTEGER  |           |        |      |X    |
    +--------------+---------+-----------+--------+------+-----+
    |bar           |REAL     |           |        |      |     |
    +--------------+---------+-----------+--------+------+-----+
    |hoge          |TEXT     |           |        |      |X    |
    +--------------+---------+-----------+--------+------+-----+

.. table:: constraints (0 records)

    +--------------+---------+-----------+--------+------+-----+
    |Attribute name|Data type|Primary key|Not NULL|Unique|Index|
    +==============+=========+===========+========+======+=====+
    |primarykey_id |INTEGER  |X          |        |      |     |
    +--------------+---------+-----------+--------+------+-----+
    |notnull_value |REAL     |           |X       |      |     |
    +--------------+---------+-----------+--------+------+-----+
    |unique_value  |INTEGER  |           |        |X     |     |
    +--------------+---------+-----------+--------+------+-----+

Dump Schema as Text

Sample Code:
for verbosity_level in range(6):
    print("----- dump schema text: verbosity_level={} -----".format(
        verbosity_level))
    extractor = sqliteschema.SqliteSchemaExtractor(
        sqlite_db_path, verbosity_level=verbosity_level,
        output_format="text")
    print(extractor.dumps())
Output:
----- dump schema text: verbosity_level=0 -----
sampletable0
sampletable1
constraints

----- dump schema text: verbosity_level=1 -----
sampletable0 (attr_a, attr_b)
sampletable1 (foo, bar, hoge)
constraints (primarykey_id, notnull_value, unique_value)

----- dump schema text: verbosity_level=2 -----
sampletable0 (attr_a INTEGER, attr_b INTEGER)
sampletable1 (foo INTEGER, bar REAL, hoge TEXT)
constraints (primarykey_id INTEGER, notnull_value REAL, unique_value INTEGER)

----- dump schema text: verbosity_level=3 -----
sampletable0 (attr_a INTEGER, attr_b INTEGER)
sampletable1 (foo INTEGER, bar REAL, hoge TEXT)
constraints (primarykey_id INTEGER PRIMARY KEY, notnull_value REAL NOT NULL, unique_value INTEGER UNIQUE)

----- dump schema text: verbosity_level=4 -----
sampletable0 (
    attr_a INTEGER,
    attr_b INTEGER
)

sampletable1 (
    foo INTEGER,
    bar REAL,
    hoge TEXT
)

constraints (
    primarykey_id INTEGER PRIMARY KEY,
    notnull_value REAL NOT NULL,
    unique_value INTEGER UNIQUE
)


----- dump schema text: verbosity_level=5 -----
sampletable0 (
    attr_a INTEGER,
    attr_b INTEGER
)

sampletable1 (
    foo INTEGER,
    bar REAL,
    hoge TEXT
)
CREATE INDEX sampletable1_foo_index ON sampletable1('foo')
CREATE INDEX sampletable1_hoge_index ON sampletable1('hoge')

constraints (
    primarykey_id INTEGER PRIMARY KEY,
    notnull_value REAL NOT NULL,
    unique_value INTEGER UNIQUE
)

Dependencies

Python 2.7+ or 3.3+

Test dependencies

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqliteschema-0.9.4.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqliteschema-0.9.4-py2.py3-none-any.whl (12.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sqliteschema-0.9.4.tar.gz.

File metadata

  • Download URL: sqliteschema-0.9.4.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sqliteschema-0.9.4.tar.gz
Algorithm Hash digest
SHA256 9ee2f9eb7c284d8cb05dd5614d2a40f8f6b574e91233e3d508193c97dc3d3c98
MD5 26ffbf8d068aa9e243e1796771b6116d
BLAKE2b-256 022fbf6defd06372d78f70bbb817690a9fa3eeec36d1293ad9ab1121210dc18f

See more details on using hashes here.

File details

Details for the file sqliteschema-0.9.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sqliteschema-0.9.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 83417a80193a7d4faabde23a3c3c106bfed6be8ab3f4405e3403931839169854
MD5 822d531b2b24f575ee59923b66b1504c
BLAKE2b-256 10a48463bb8b83d377be3d6dbe1379227750f4c788a755f844ef5a7dd2cb9708

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page