Skip to main content
# sql_insert_writer

[![PyPI Status](https://img.shields.io/pypi/v/sql_insert_writer.svg)](https://pypi.python.org/pypi/sql_insert_writer)
[![CircleCI](https://circleci.com/gh/18F/sql_insert_writer.svg?style=svg)](https://circleci.com/gh/18F/sql_insert_writer)
[![Code Climate](https://codeclimate.com/github/18F/sql_insert_writer/badges/gpa.svg)](https://codeclimate.com/github/18F/sql_insert_writer)
[![Test Coverage](https://codeclimate.com/github/18F/sql_insert_writer/badges/coverage.svg)](https://codeclimate.com/github/18F/sql_insert_writer/coverage)
[![Dependency Status](https://gemnasium.com/badges/github.com/18F/sql_insert_writer.svg)](https://gemnasium.com/github.com/18F/sql_insert_writer)

Helps generate highly readable SQL INSERT statements

Calling with one table name creates an `INSERT INTO... VALUES` statement:

```
$ sql_insert_writer pet

INSERT INTO pet (
id,
name,
species_name,
planet,
kg
)
VALUES
(
DEFAULT, -- ==> id
DEFAULT, -- ==> name
DEFAULT, -- ==> species_name
DEFAULT, -- ==> planet
DEFAULT -- ==> kg
)
```

If more table names are added, will generate an `INSERT INTO... SELECT FROM`
statement, matching as many column names as it can between the destination
and source table(s):

```

$ sql_insert_writer pet animal

INSERT INTO pet (
id,
name,
species_name,
planet,
kg
)
SELECT
id, -- ==> id
name, -- ==> name
species_name, -- ==> species_name
planet, -- ==> planet
DEFAULT -- ==> kg
FROM animal
```

## More usage examples

https://github.com/18F/sql_insert_writer

## Rationale

The syntax of `INSERT` statements makes it difficult to tell which destination columns a value is intended for,
especially in inserts with many columns. (Our five-column example is not bad, but imagine fifty columns!)

Comments can clarify the link between data source and destination, but adding those comments manually is tedious and error-prone.

Explicitly listing the destination columns of an `INSERT` is another best practice often skipped due to tedium.

The output of `sql_insert_writer` will rarely be fully ready to execute, but it should save the bulk of the typing.

## Features

- Supports PostgreSQL, SQLite, MySQL
- Accepts [SQLAlchemy database URLs](http://docs.sqlalchemy.org/en/latest/core/engines.html) with `--db` option. Defaults to environment variable `$DATABASE_URL`.
- Any number of source tables; columns chosen in order specified
- Any number of tuples in `VALUES` clause with `--tuples` option
- Explicitly cast to destination column type with `--cast` option

## Installation

[Installation instructions](docs/installation.rst)

Development installation instructions, so that
you can modify the code and contribute your
improvements back to the project, are included
in the [CONTRIBUTING documentation](CONTRIBUTING.rst).

## Planned features

- Support for more databases
- Approximate column name matches
- Omit inserts into auto-incrementing primary key columns
- Pre-fill JOIN clauses with foreign keys where possible

## Limitations

We do not deal well with case-sensitive table or column names; for lo, they are an abomination unto Codd.

## Credits

This package was created with [Cookiecutter](https://github.com/audreyr/cookiecutter)
and the [18F/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage)
project template.

## Public domain

This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.rst):

> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/).
>
> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.


=======
History
=======

0.1.0 (2017-10-12)
------------------

* First release on PyPI.


Metadata

Release files for sql-insert-writer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql-insert-writer 0.1.0
File Size Uploaded
sql_insert_writer-0.1.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sql-insert-writer 0.1.0
File Interpreter ABI Platform
sql_insert_writer-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 21.0 kB

Release files / sql_insert_writer-0.1.0.tar.gz

Download URL sql_insert_writer-0.1.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
30077563e9e351fcaff786aa5b28aff05449e194e1ff5b8d0365c65cb0949641
BLAKE2b-256 checksum
How to use checksums
46de76245247f47adc4af1c275a1e5f02bcfb59b62e7b802e4a1558a61e43f70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sql_insert_writer-0.1.0-py2.py3-none-any.whl

Download URL sql_insert_writer-0.1.0-py2.py3-none-any.whl
Size 9.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2199c0193c62363e426c987d65d171feba8a68237d3da32b1841144f3c32cc0b
BLAKE2b-256 checksum
How to use checksums
e71e4546725ccf246ddfc13b3a598537d3985df10562213e2deb24db8bc48d75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page