项目作者: simonw

项目描述 :
Python CLI utility and library for manipulating SQLite databases
高级语言: Python
项目地址: git://github.com/simonw/sqlite-utils.git
创建时间: 2018-07-14T03:21:46Z
项目社区:https://github.com/simonw/sqlite-utils

开源协议:Apache License 2.0

下载


sqlite-utils

PyPI
Changelog
Python 3.x
Tests
Documentation Status
codecov
License
discord

Python CLI utility and library for manipulating SQLite databases.

Some feature highlights

Read more on my blog, in this series of posts on New features in sqlite-utils and other entries tagged sqliteutils.

Installation

  1. pip install sqlite-utils

Or if you use Homebrew for macOS:

  1. brew install sqlite-utils

Using as a CLI tool

Now you can do things with the CLI utility like this:

  1. $ sqlite-utils memory dogs.csv "select * from t"
  2. [{"id": 1, "age": 4, "name": "Cleo"},
  3. {"id": 2, "age": 2, "name": "Pancakes"}]
  4. $ sqlite-utils insert dogs.db dogs dogs.csv --csv
  5. [####################################] 100%
  6. $ sqlite-utils tables dogs.db --counts
  7. [{"table": "dogs", "count": 2}]
  8. $ sqlite-utils dogs.db "select id, name from dogs"
  9. [{"id": 1, "name": "Cleo"},
  10. {"id": 2, "name": "Pancakes"}]
  11. $ sqlite-utils dogs.db "select * from dogs" --csv
  12. id,age,name
  13. 1,4,Cleo
  14. 2,2,Pancakes
  15. $ sqlite-utils dogs.db "select * from dogs" --table
  16. id age name
  17. ---- ----- --------
  18. 1 4 Cleo
  19. 2 2 Pancakes

You can import JSON data into a new database table like this:

  1. $ curl https://api.github.com/repos/simonw/sqlite-utils/releases \
  2. | sqlite-utils insert releases.db releases - --pk id

Or for data in a CSV file:

  1. $ 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:

  1. $ cat dogs.csv | sqlite-utils memory - "select name, age from stdin"

See the full CLI documentation for comprehensive coverage of many more commands.

Using as a library

You can also import sqlite_utils and use it as a Python library like this:

  1. import sqlite_utils
  2. db = sqlite_utils.Database("demo_database.db")
  3. # This line creates a "dogs" table if one does not already exist:
  4. db["dogs"].insert_all([
  5. {"id": 1, "age": 4, "name": "Cleo"},
  6. {"id": 2, "age": 2, "name": "Pancakes"}
  7. ], pk="id")

Check out the full library documentation for everything else you can do with the Python library.

  • Datasette: A tool for exploring and publishing data
  • csvs-to-sqlite: Convert CSV files into a SQLite database
  • db-to-sqlite: CLI tool for exporting a MySQL or PostgreSQL database as a SQLite file
  • dogsheep: A family of tools for personal analytics, built on top of sqlite-utils