mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
81 lines
2.2 KiB
ReStructuredText
81 lines
2.2 KiB
ReStructuredText
.. _python_api:
|
|
|
|
================================
|
|
sqlite-utils command-line tool
|
|
================================
|
|
|
|
The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways.
|
|
|
|
Listing tables
|
|
==============
|
|
|
|
You can list the names of tables in a database using the ``table_names`` subcommand::
|
|
|
|
$ sqlite-utils table_names mydb.db
|
|
dogs
|
|
cats
|
|
chickens
|
|
|
|
If you just want to see the FTS4 tables, you can use ``--fts4`` (or ``--fts5`` for FTS5 tables)::
|
|
|
|
$ sqlite-utils table_names --fts4 docs.db
|
|
docs_fts
|
|
|
|
Inserting data
|
|
==============
|
|
|
|
If you have data as JSON, you can use ``sqlite-utils insert tablename`` to insert it into a database. The table will be created with the correct (automatically detected) columns if it does not already exist.
|
|
|
|
You can pass in a single JSON object or a list of JSON objects, either as a filename or piped directly to standard-in.
|
|
|
|
Here's the simplest possible example::
|
|
|
|
$ echo '{"name": "Cleo", "age": 4}' | sqlite-utils insert dogs.db dogs -
|
|
|
|
To specify a column as the primary key, use ``--pk=column_name``.
|
|
|
|
If you feed it a JSON list it will insert multiple records. For example, if ``dogs.json`` looks like this::
|
|
|
|
[
|
|
{
|
|
"id": 1,
|
|
"name": "Cleo",
|
|
"age": 4
|
|
},
|
|
{
|
|
"id": 2,
|
|
"name": "Pancakes",
|
|
"age": 2
|
|
},
|
|
{
|
|
"id": 3,
|
|
"name": "Toby",
|
|
"age": 6
|
|
}
|
|
]
|
|
|
|
You can import all three records into an automatically created ``dogs`` table and set the ``id`` column as the primary key like so::
|
|
|
|
$ sqlite-utils insert dogs.db dogs.json --pk=id
|
|
|
|
Vacuum
|
|
======
|
|
|
|
You can run VACUUM to optimize your database like so::
|
|
|
|
$ sqlite-utils vacuum mydb.db
|
|
|
|
Optimize
|
|
========
|
|
|
|
The optimize command can dramatically reduce the size of your database if you are using SQLite full-text search. It runs OPTIMIZE against all of our FTS4 and FTS5 tables, then runs VACUUM.
|
|
|
|
If you just want to run OPTIMIZE without the VACUUM, use the ``--no-vacuum`` flag.
|
|
|
|
::
|
|
|
|
# Optimize all FTS tables and then VACUUM
|
|
$ sqlite-utils optimize mydb.db
|
|
|
|
# Optimize but skip the VACUUM
|
|
$ sqlite-utils optimize --no-vacuum mydb.db
|