Skip to main content

Create SQLite database tables automatically from data

Project description

# DatatoTable Datatotable converts data into SQLites tables automatically. The package is most useful when the name and number of columns in a table are large or unknown ahead of time as SQLalchemy’s Object Relational mapper (ORM) requires manual generation of tables and their columns. Datatotable automates this process. Yet, it still allows manual additions of constraints, foreign keys, and other properties as desired.

## Quick Start The minimal use case of datatotable creates a table from a data source, and then inserts the data. This example walks through this process. Datatotable functions through two classes Database, which provides access to the database, and DataOperator, which interprets data for use with SQLalchemy. First, import these classes. Next, import a SQLalchemy Session class for inserts and tempfile to create a temporary directory.

`python from datatotable.database import Database from datatotable.data import DataOperator from sqlalchemy.orm import Session import tempfile ` To connect to or create a database, specify the name and directory of the database on Database’s instantiation. Then, connect a session to the database via Database’s engine property. `python temp_dir = tempfile.TemporaryDirectory() db = database.Database(name="sample", directory=temp_dir.name) session = Session(db.engine) `

Next, pass data to DataOperator. The DataOperator’s column property reads the data and returns a dictionary with column names as keys and lists of column attributes as values. `python raw_data = {"col1": [1,2,3,4], "col2": ["hello", "world", "from", "computer"], "col3": [10.1, 13.5, 23.2, 98.4]} data = datatotable.data.DataOperator(raw_data) columns = data.columns print(columns) >>> {'col1': [<class 'sqlalchemy.sql.sqltypes.Integer'>], 'col2': [<class 'sqlalchemy.sql.sqltypes.String'>], 'col3': [<class 'sqlalchemy.sql.sqltypes.Float'>]} `

Pass the columns to Database’s map_table() function to create a mapped SQLalchemy Base class. After the table is mapped, call the create_tables() function to create the table in the database. `python db.map_table(tbl_name="example_tbl", column_types=columns) db.create_tables() `

With the table created, access the table via the Database.table_mappings property. The table mapping can be used for queries or mapping data for inserts. The row property of DataOperator returns a list of dictionaries where each dictionary is a row in the database. Use ** notation to unpack each dictionary into a table object. A list comprehension, as used here, can create a list of table objects. Add these objects using Session.add_all(). `python example_tbl = db.table_mappings["example_tbl"] example_rows = [example_tbl(**row) for row in data.rows] session.add_all(example_rows) session.commit() `

Now, check if the data is in the database. `python print(session.query(example_tbl).count()) >>> 4 `

## Version 0.3 This version is the first valid version uploaded to PyPi. It provides access to a database, automatically creates tables, and coerces data into a format that can be inserted into a table. It passes all tests in the test suite, but the test suite will need to be fleshed out as users come across problems not anticipated currently.

## Future Development ### Documentation 1. Use Sphinx to create API documentation on Read the Docs 2. A thorough tutorial to explain how constraints and relationships are established ### Functionality 1. Add functionality to relate SQLalchemy ORM objects when tables are created with foreign keys. This is a manual process currently. 2. Allow simultaneous creation of multiple tables. Tables must be created one by one currently.

## Author Spencer Weston

spencerweston3214@gmail.com

## License MIT

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

datatotable-0.3.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

datatotable-0.3.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file datatotable-0.3.1.tar.gz.

File metadata

  • Download URL: datatotable-0.3.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for datatotable-0.3.1.tar.gz
Algorithm Hash digest
SHA256 51ff2a6ce526873edcb145e9b93f5cd6f00b5005e19858b0e64b8cde53f35446
MD5 cdfd2b311ac4722afb611ec5cec25336
BLAKE2b-256 0c4d17491722390bb1f2916d091f698075562e129d43e258e2c6058c0d5e804d

See more details on using hashes here.

File details

Details for the file datatotable-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: datatotable-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for datatotable-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5f817c8965159e02b4912e12de02650900321a3938cf873872b386b41e2cb45
MD5 7f35ab87bcf87a13079cef54c88a9bc6
BLAKE2b-256 b6ba317ebb9ec463c1297c5edf7aa04e4a9e96e081cf30eada190fecfef7486e

See more details on using hashes here.

Supported by

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