sqlite-utils/README.md

101 lines
5.4 KiB
Markdown
Raw Normal View History

2018-07-13 20:56:21 -07:00
# sqlite-utils
2018-07-31 08:38:50 -07:00
[![PyPI](https://img.shields.io/pypi/v/sqlite-utils.svg)](https://pypi.org/project/sqlite-utils/)
2021-08-09 15:44:26 -07:00
[![Changelog](https://img.shields.io/github/v/release/simonw/sqlite-utils?include_prereleases&label=changelog)](https://sqlite-utils.datasette.io/en/stable/changelog.html)
[![Python 3.x](https://img.shields.io/pypi/pyversions/sqlite-utils.svg?logo=python&logoColor=white)](https://pypi.org/project/sqlite-utils/)
2020-08-28 15:41:29 -07:00
[![Tests](https://github.com/simonw/sqlite-utils/workflows/Test/badge.svg)](https://github.com/simonw/sqlite-utils/actions?query=workflow%3ATest)
2021-08-09 15:44:26 -07:00
[![Documentation Status](https://readthedocs.org/projects/sqlite-utils/badge/?version=stable)](http://sqlite-utils.datasette.io/en/stable/?badge=stable)
2021-06-16 17:12:11 -07:00
[![codecov](https://codecov.io/gh/simonw/sqlite-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/simonw/sqlite-utils)
2020-08-10 14:04:32 -07:00
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/sqlite-utils/blob/main/LICENSE)
2022-08-02 14:15:52 -07:00
[![discord](https://img.shields.io/discord/823971286308356157?label=discord)](https://discord.gg/Ass7bCAMDw)
2018-07-31 08:38:50 -07:00
2019-01-25 18:21:55 -08:00
Python CLI utility and library for manipulating SQLite databases.
## Some feature highlights
- [Pipe JSON](https://sqlite-utils.datasette.io/en/stable/cli.html#inserting-json-data) (or [CSV or TSV](https://sqlite-utils.datasette.io/en/stable/cli.html#inserting-csv-or-tsv-data)) directly into a new SQLite database file, automatically creating a table with the appropriate schema
2022-02-17 07:39:54 -08:00
- [Run in-memory SQL queries](https://sqlite-utils.datasette.io/en/stable/cli.html#querying-data-directly-using-an-in-memory-database), including joins, directly against data in CSV, TSV or JSON files and view the results
- [Configure SQLite full-text search](https://sqlite-utils.datasette.io/en/stable/cli.html#configuring-full-text-search) against your database tables and run search queries against them, ordered by relevance
- Run [transformations against your tables](https://sqlite-utils.datasette.io/en/stable/cli.html#transforming-tables) to make schema changes that SQLite `ALTER TABLE` does not directly support, such as changing the type of a column
- [Extract columns](https://sqlite-utils.datasette.io/en/stable/cli.html#extracting-columns-into-a-separate-table) into separate tables to better normalize your existing data
2026-07-06 22:26:58 -07:00
- [Manage database migrations](https://sqlite-utils.datasette.io/en/stable/migrations.html) using Python migration files and the `sqlite-utils migrate` command
2023-08-17 19:28:08 -07:00
- [Install plugins](https://sqlite-utils.datasette.io/en/stable/plugins.html) to add custom SQL functions and additional features
2026-07-06 22:26:58 -07:00
Upgrading from sqlite-utils 3.x? See the [4.0 upgrade guide](https://sqlite-utils.datasette.io/en/stable/upgrading.html#upgrading-from-3-x-to-4-0).
Read more on my blog, in this series of posts on [New features in sqlite-utils](https://simonwillison.net/series/sqlite-utils-features/) and other [entries tagged sqlite-utils](https://simonwillison.net/tags/sqlite-utils/).
2019-02-24 19:45:38 -08:00
2020-04-17 16:59:47 -07:00
## Installation
2018-07-31 08:38:50 -07:00
2020-05-11 12:23:11 -07:00
pip install sqlite-utils
2018-07-31 08:38:50 -07:00
Or if you use [Homebrew](https://brew.sh/) for macOS:
brew install sqlite-utils
2020-04-17 16:59:47 -07:00
## Using as a CLI tool
2019-07-03 11:04:03 -07:00
Now you can do things with the CLI utility like this:
2019-01-25 18:21:55 -08:00
$ sqlite-utils memory dogs.csv "select * from t"
[{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}]
$ sqlite-utils insert dogs.db dogs dogs.csv --csv
2021-08-18 16:01:00 -07:00
[####################################] 100%
$ sqlite-utils tables dogs.db --counts
[{"table": "dogs", "count": 2}]
2019-01-25 18:21:55 -08:00
2021-08-18 16:02:07 -07:00
$ sqlite-utils dogs.db "select id, name from dogs"
[{"id": 1, "name": "Cleo"},
{"id": 2, "name": "Pancakes"}]
$ sqlite-utils dogs.db "select * from dogs" --csv
2019-01-25 18:21:55 -08:00
id,age,name
1,4,Cleo
2,2,Pancakes
2019-02-24 15:11:51 -08:00
$ sqlite-utils dogs.db "select * from dogs" --table
id age name
---- ----- --------
1 4 Cleo
2 2 Pancakes
2021-02-14 13:36:43 -08:00
You can import JSON data into a new database table like this:
2020-04-17 16:59:47 -07:00
$ curl https://api.github.com/repos/simonw/sqlite-utils/releases \
2020-09-30 16:29:27 -04:00
| sqlite-utils insert releases.db releases - --pk id
2020-04-17 16:59:47 -07:00
2021-02-14 13:36:43 -08:00
Or for data in a CSV file:
$ sqlite-utils insert dogs.db dogs dogs.csv --csv
`sqlite-utils memory` lets you import CSV or JSON data into an in-memory database and run SQL queries against it in a single command:
2021-08-18 16:02:55 -07:00
$ cat dogs.csv | sqlite-utils memory - "select name, age from stdin"
2021-02-14 13:36:43 -08:00
See the [full CLI documentation](https://sqlite-utils.datasette.io/en/stable/cli.html) for comprehensive coverage of many more commands.
2020-04-17 16:59:47 -07:00
## Using as a library
You can also `import sqlite_utils` and use it as a Python library like this:
2019-07-03 11:04:03 -07:00
```python
import sqlite_utils
db = sqlite_utils.Database("demo_database.db")
# This line creates a "dogs" table if one does not already exist:
db["dogs"].insert_all([
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}
], pk="id")
```
Check out the [full library documentation](https://sqlite-utils.datasette.io/en/stable/python-api.html) for everything else you can do with the Python library.
2018-07-31 08:38:50 -07:00
2020-04-17 16:59:47 -07:00
## Related projects
2018-07-31 08:38:50 -07:00
2020-12-29 13:33:25 -08:00
* [Datasette](https://datasette.io/): A tool for exploring and publishing data
2018-07-31 08:38:50 -07:00
* [csvs-to-sqlite](https://github.com/simonw/csvs-to-sqlite): Convert CSV files into a SQLite database
* [db-to-sqlite](https://github.com/simonw/db-to-sqlite): CLI tool for exporting a MySQL or PostgreSQL database as a SQLite file
2020-04-17 16:59:47 -07:00
* [dogsheep](https://dogsheep.github.io/): A family of tools for personal analytics, built on top of `sqlite-utils`