项目作者: simonw

项目描述 :
Utility for converting YAML files to SQLite
高级语言: Python
项目地址: git://github.com/simonw/yaml-to-sqlite.git
创建时间: 2019-03-14T04:49:08Z
项目社区:https://github.com/simonw/yaml-to-sqlite

开源协议:Apache License 2.0

下载


yaml-to-sqlite

PyPI
Changelog
Tests
License

Load the contents of a YAML file into a SQLite database table.

  1. $ yaml-to-sqlite --help
  2. Usage: yaml-to-sqlite [OPTIONS] DB_PATH TABLE YAML_FILE
  3. Convert YAML files to SQLite
  4. Options:
  5. --version Show the version and exit.
  6. --pk TEXT Column to use as a primary key
  7. --single-column TEXT If YAML file is a list of values, populate this column
  8. --help Show this message and exit.

Usage

Given a news.yml file containing the following:

  1. - date: 2021-06-05
  2. body: |-
  3. [Datasette 0.57](https://docs.datasette.io/en/stable/changelog.html#v0-57) is out with an important security patch.
  4. - date: 2021-05-10
  5. body: |-
  6. [Django SQL Dashboard](https://simonwillison.net/2021/May/10/django-sql-dashboard/) is a new tool that brings a useful authenticated subset of Datasette to Django projects that are built on top of PostgreSQL.

Running this command:

  1. $ yaml-to-sqlite news.db stories news.yml

Will create a database file with this schema:

  1. $ sqlite-utils schema news.db
  2. CREATE TABLE [stories] (
  3. [date] TEXT,
  4. [body] TEXT
  5. );

The --pk option can be used to set a column as the primary key for the table:

  1. $ yaml-to-sqlite news.db stories news.yml --pk date
  2. $ sqlite-utils schema news.db
  3. CREATE TABLE [stories] (
  4. [date] TEXT PRIMARY KEY,
  5. [body] TEXT
  6. );

Single column YAML lists

The --single-column option can be used when the YAML file is a list of values, for example a file called dogs.yml containing the following:

  1. - Cleo
  2. - Pancakes
  3. - Nixie

Running this command:

  1. $ yaml-to-sqlite dogs.db dogs.yaml --single-column=name

Will create a single dogs table with a single name column that is the primary key:

  1. $ sqlite-utils schema dogs.db
  2. CREATE TABLE [dogs] (
  3. [name] TEXT PRIMARY KEY
  4. );
  5. $ sqlite-utils dogs.db 'select * from dogs' -t
  6. name
  7. --------
  8. Cleo
  9. Pancakes
  10. Nixie