Skip to main content

Tool for running transformations on columns in a SQLite database.

Project description

sqlite-transform

PyPI Changelog Tests License

Tool for running transformations on columns in a SQLite database.

How to install

pip install sqlite-transform

parsedate and parsedatetime

These subcommands will run all values in the specified column through dateutils.parser.parse() and replace them with the result, formatted as an ISO timestamp or ISO date.

For example, if a row in the database has an opened column which contains 10/10/2019 08:10:00 PM, running the following command:

sqlite-transform parsedatetime my.db mytable opened

Will result in that value being replaced by 2019-10-10T20:10:00.

Using the parsedate subcommand here would result in 2019-10-10 instead.

In the case of ambiguous dates such as 03/04/05 these commands both default to assuming American-style mm/dd/yy format. You can pass --dayfirst to specify that the day should be assumed to be first, or --yearfirst for the year.

jsonsplit

The jsonsplit subcommand takes columns that contain a comma-separated list, for example a tags column containing records like "trees,park,dogs" and converts it into a JSON array ["trees", "park", "dogs"].

This is useful for taking advantage of Datasette's Facet by JSON array feature.

sqlite-transform jsonsplit my.db mytable tags

It defaults to splitting on commas, but you can specify a different delimiter character using the --delimiter option, for example:

sqlite-transform jsonsplit \
    my.db mytable tags --delimiter ';'

Values within the array will be treated as strings, so a column containing 123,552,775 will be converted into the JSON array ["123", "552", "775"].

You can specify a different type for these values using --type int or --type float, for example:

sqlite-transform jsonsplit \
    my.db mytable tags --type int

This will result in that column being converted into [123, 552, 775].

lambda for executing your own code

The lambda subcommand lets you specify Python code which will be executed against the column.

Here's how to convert a column to uppercase:

sqlite-transform lambda my.db mytable mycolumn --code='str(value).upper()'

The code you provide will be compiled into a function that takes value as a single argument. You can break your function body into multiple lines, provided the last line is a return statement:

sqlite-transform lambda my.db mytable mycolumn --code='value = str(value)
return value.upper()'

You can also specify Python modules that should be imported and made available to your code using one or more --import options:

sqlite-transform lambda my.db mytable mycolumn \
    --code='"\n".join(textwrap.wrap(value, 10))' \
    --import=textwrap

The --dry-run option will output a preview of the transformation against the first ten rows, without modifying the database.

Saving the result to a separate column

Each of these commands accepts optional --output and --output-type options. These can be used to save the result of the transformation to a separate column, which will be created if the column does not already exist.

To save the result of jsonsplit to a new column called json_tags, use the following:

sqlite-transform jsonsplit my.db mytable tags \
  --output json_tags

The type of the created column defaults to text, but a different column type can be specified using --output-type. This example will create a new floating point column called float_id with a copy of each item's ID increased by 0.5:

sqlite-transform lambda my.db mytable id \
  --code 'float(value) + 0.5' \
  --output float_id \
  --output-type float

Disabling the progress bar

By default each command will show a progress bar. Pass -s or --silent to hide that progress bar.

Terminology warning

This tool uses the word "transform" to mean something different from the sqlite-utils transform command.

In sqlite-utils, "transform" is used to describe running complex alter table statements, see Executing advanced ALTER TABLE operations in SQLite

sqlite-transform uses the term to describe performing a transformation or conversion on every value in a column.

I apologize for this confusion! I wish I had used different names for these two concepts.

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

sqlite-transform-1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

sqlite_transform-1.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlite-transform-1.1.tar.gz.

File metadata

  • Download URL: sqlite-transform-1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for sqlite-transform-1.1.tar.gz
Algorithm Hash digest
SHA256 a3a193927dd8c59ca92d015a61d8abba204a01434566087103924e3d29f6fb6e
MD5 2faca0dc3c7afd7ef9312df051a5e64b
BLAKE2b-256 d5dc2b8dd18fa3c8468d3c1abb3b0ac162b3bd0cc2f3af91ba6b3cdc92ae3068

See more details on using hashes here.

File details

Details for the file sqlite_transform-1.1-py3-none-any.whl.

File metadata

  • Download URL: sqlite_transform-1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for sqlite_transform-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b4f9d7599395d3aabb8bb136a4207f6e3db9ac516b7e0abb69cd23505d2a304
MD5 04048b614ef79bc5d4f0c567503f3b48
BLAKE2b-256 1898500c282a3b6cb60b140419b5f317469d6d8149ccfad62b0ff9dedd2c7684

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