mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 18:04:32 +02:00
'sqlite-utils insert tablename file.json' command
This commit is contained in:
parent
1c683076d3
commit
9e74289397
4 changed files with 104 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import click
|
||||
import sqlite_utils
|
||||
import json
|
||||
|
||||
|
||||
@click.group()
|
||||
|
|
@ -55,3 +56,20 @@ def optimize(path, no_vacuum):
|
|||
db[table].optimize()
|
||||
if not no_vacuum:
|
||||
db.vacuum()
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
|
||||
required=True,
|
||||
)
|
||||
@click.argument("table")
|
||||
@click.argument("json_file", type=click.File(), required=True)
|
||||
@click.option("--pk", help="Column to use as the primary key, e.g. id")
|
||||
def insert(path, table, json_file, pk):
|
||||
db = sqlite_utils.Database(path)
|
||||
docs = json.load(json_file)
|
||||
if isinstance(docs, dict):
|
||||
docs = [docs]
|
||||
db[table].insert_all(docs, pk=pk)
|
||||
|
|
|
|||
|
|
@ -128,6 +128,10 @@ class Table:
|
|||
).fetchall()
|
||||
return [Column(*row) for row in rows]
|
||||
|
||||
@property
|
||||
def pks(self):
|
||||
return [column.name for column in self.columns if column.is_pk]
|
||||
|
||||
@property
|
||||
def foreign_keys(self):
|
||||
fks = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue