A Python library to dump table schema of a SQLite database file.
Project Links
Meta
Author: Tsuyoshi Hombashi
Requires Python: >=3.9
Classifiers
Development Status
- 5 - Production/Stable
Environment
- Console
Intended Audience
- Developers
- Information Technology
License
- OSI Approved :: MIT License
Operating System
- OS Independent
Programming Language
- Python :: 3
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: 3 :: Only
- Python :: Implementation :: CPython
- Python :: Implementation :: PyPy
Topic
- Software Development :: Libraries
- Software Development :: Libraries :: Python Modules
- Database
- Terminals
Summary
sqliteschema is a Python library to dump table schema of a SQLite database file.
Installation
Install from PyPI
pip install sqliteschema
Install optional dependencies
pip install sqliteschema[cli] # to use CLI pip install sqliteschema[dumps] # to use dumps method pip install sqliteschema[logging] # to use logging
Install from PPA (for Ubuntu)
sudo add-apt-repository ppa:thombashi/ppa sudo apt update sudo apt install python3-sqliteschema
Usage
Full example source code can be found at examples/get_table_schema.py
Extract SQLite Schemas as dict
- Sample Code:
import json import sqliteschema extractor = sqliteschema.SQLiteSchemaExtractor(sqlite_db_path) print( "--- dump all of the table schemas into a dictionary ---\n{}\n".format( json.dumps(extractor.fetch_database_schema_as_dict(), indent=4) ) ) print( "--- dump a specific table schema into a dictionary ---\n{}\n".format( json.dumps(extractor.fetch_table_schema("sampletable1").as_dict(), indent=4) ) )- Output:
--- dump all of the table schemas into a dictionary --- { "sampletable0": [ { "Field": "attr_a", "Index": false, "Type": "INTEGER", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" }, { "Field": "attr_b", "Index": false, "Type": "INTEGER", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" } ], "sampletable1": [ { "Field": "foo", "Index": true, "Type": "INTEGER", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" }, { "Field": "bar", "Index": false, "Type": "REAL", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" }, { "Field": "hoge", "Index": true, "Type": "TEXT", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" } ], "constraints": [ { "Field": "primarykey_id", "Index": true, "Type": "INTEGER", "Nullable": "YES", "Key": "PRI", "Default": "NULL", "Extra": "" }, { "Field": "notnull_value", "Index": false, "Type": "REAL", "Nullable": "NO", "Key": "", "Default": "", "Extra": "" }, { "Field": "unique_value", "Index": true, "Type": "INTEGER", "Nullable": "YES", "Key": "UNI", "Default": "NULL", "Extra": "" } ] } --- dump a specific table schema into a dictionary --- { "sampletable1": [ { "Field": "foo", "Index": true, "Type": "INTEGER", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" }, { "Field": "bar", "Index": false, "Type": "REAL", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" }, { "Field": "hoge", "Index": true, "Type": "TEXT", "Nullable": "YES", "Key": "", "Default": "NULL", "Extra": "" } ] }
Extract SQLite Schemas as Tabular Text
Table schemas can be output with the dumps method. The dumps method requires an additional package that can be installed as follows:
pip install sqliteschema[dumps]
Usage is as follows:
- Sample Code:
import sqliteschema extractor = sqliteschema.SQLiteSchemaExtractor(sqlite_db_path) for verbosity_level in range(2): print("--- dump all of the table schemas with a tabular format: verbosity_level={} ---".format( verbosity_level)) print(extractor.dumps(output_format="markdown", verbosity_level=verbosity_level)) for verbosity_level in range(2): print("--- dump a specific table schema with a tabular format: verbosity_level={} ---".format( verbosity_level)) print(extractor.fetch_table_schema("sampletable1").dumps( output_format="markdown", verbosity_level=verbosity_level))- Output:
--- dump all of the table schemas with a tabular format: verbosity_level=0 --- # sampletable0 | Field | Type | | ------ | ------- | | attr_a | INTEGER | | attr_b | INTEGER | # sampletable1 | Field | Type | | ----- | ------- | | foo | INTEGER | | bar | REAL | | hoge | TEXT | # constraints | Field | Type | | ------------- | ------- | | primarykey_id | INTEGER | | notnull_value | REAL | | unique_value | INTEGER | --- dump all of the table schemas with a tabular format: verbosity_level=1 --- # sampletable0 | Field | Type | Nullable | Key | Default | Index | Extra | | ------ | ------- | -------- | --- | ------- | :---: | ----- | | attr_a | INTEGER | YES | | NULL | | | | attr_b | INTEGER | YES | | NULL | | | # sampletable1 | Field | Type | Nullable | Key | Default | Index | Extra | | ----- | ------- | -------- | --- | ------- | :---: | ----- | | foo | INTEGER | YES | | NULL | X | | | bar | REAL | YES | | NULL | | | | hoge | TEXT | YES | | NULL | X | | # constraints | Field | Type | Nullable | Key | Default | Index | Extra | | ------------- | ------- | -------- | --- | ------- | :---: | ----- | | primarykey_id | INTEGER | YES | PRI | NULL | X | | | notnull_value | REAL | NO | | | | | | unique_value | INTEGER | YES | UNI | NULL | X | | --- dump a specific table schema with a tabular format: verbosity_level=0 --- # sampletable1 | Field | Type | | ----- | ------- | | foo | INTEGER | | bar | REAL | | hoge | TEXT | --- dump a specific table schema with a tabular format: verbosity_level=1 --- # sampletable1 | Field | Type | Nullable | Key | Default | Index | Extra | | ----- | ------- | -------- | --- | ------- | :---: | ----- | | foo | INTEGER | YES | | NULL | X | | | bar | REAL | YES | | NULL | | | | hoge | TEXT | YES | | NULL | X | |
CLI Usage
- Sample Code:
pip install --upgrade sqliteschema[cli] python3 -m sqliteschema <PATH/TO/SQLITE_FILE>
Dependencies
Optional dependencies
- loguru
Used for logging if the package installed
- pytablewriter
Required when getting table schemas with tabular text by dumps method
2.0.1
Mar 02, 2025
2.0.0
Nov 25, 2023
1.4.0
Oct 21, 2023
1.3.0
Feb 07, 2022
1.2.1
Oct 30, 2021
1.2.0
Jun 20, 2021
1.1.0
May 28, 2021
1.0.5
Dec 13, 2020
1.0.4
Dec 09, 2020
1.0.3
Aug 04, 2020
1.0.2
Aug 03, 2020
1.0.1
Mar 21, 2020
1.0.0
Mar 20, 2020
0.17.4
Feb 16, 2020
0.17.3
Feb 14, 2020
0.17.2
Feb 11, 2020
0.17.1
Feb 09, 2020
0.17.0
Feb 09, 2020
0.16.2
May 11, 2019
0.16.1
May 05, 2019
0.16.0
May 02, 2019
0.15.4
Apr 30, 2019
0.15.3
Apr 20, 2019
0.15.2
Mar 21, 2019
0.15.1
Mar 03, 2019
0.15.0
Feb 23, 2019
0.14.5
Feb 11, 2019
0.14.4
Feb 03, 2019
0.14.3
Jan 27, 2019
0.14.2
Jan 19, 2019
0.14.1
Jan 19, 2019
0.14.0
Jan 12, 2019
0.13.9
Jan 07, 2019
0.13.8
Jan 06, 2019
0.13.7
Jan 03, 2019
0.13.6
Dec 30, 2018
0.13.5
Dec 24, 2018
0.13.4
Nov 25, 2018
0.13.3
Oct 08, 2018
0.13.2
Oct 07, 2018
0.13.1
Sep 17, 2018
0.13.0
Sep 06, 2018
0.12.1
Aug 19, 2018
0.12.0
Aug 05, 2018
0.11.3
Aug 04, 2018
0.11.2
Jul 13, 2018
0.11.1
Jul 13, 2018
0.11.0
Jul 08, 2018
0.10.2
Jul 01, 2018
0.10.1
Jun 25, 2018
0.9.7
May 27, 2018
0.9.6
Apr 30, 2018
0.9.5
Nov 19, 2017
0.9.4
Aug 01, 2017
0.9.3
Jul 31, 2017
0.9.2
Jul 02, 2017
0.9.1
Jun 25, 2017
0.9.0
May 28, 2017
0.8.0
May 03, 2017
0.7.9
May 03, 2017
0.7.8
May 02, 2017
0.7.7
Apr 30, 2017
0.7.6
Feb 26, 2017
0.7.5
Feb 25, 2017
0.7.4
Jan 08, 2017
0.7.3
Jan 01, 2017
0.7.2
Jan 01, 2017
0.7.1
Dec 31, 2016
0.7.0
Dec 30, 2016
0.6.0
Dec 30, 2016
0.5.1
Dec 17, 2016
0.5.0
Dec 17, 2016
0.4.0
Dec 11, 2016
0.3.0
Dec 11, 2016