ttafsir.sqlite_utils.insert_json (1.4.0) — module

Insert data from a file into an SQLite database table.

Authors: Tafsir Thiam (@ttafsir)

Install collection

Install with ansible-galaxy collection install ttafsir.sqlite_utils:==1.4.0


Add to requirements.yml

  collections:
    - name: ttafsir.sqlite_utils
      version: 1.4.0

Description

This module inserts data from various file formats (like JSON, CSV) into an SQLite database table.

It uses the sqlite-utils Python library to handle the operations.

The table will be created or modified as necessary based on the parameters provided.

It's designed for bulk insertions and supports various optional parameters for data transformation and control.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Insert from json file
  ttafsir.sqlite_utils.insert_json:
    db_path: network.db
    table: interfaces
    file_path: interfaces.json
    flatten: true
    alter: true

Inputs

    
nl:
    default: false
    description: Specifies if the file uses newlines as a delimiter.
    type: bool

pk:
    description: Specifies which column should be the primary key when creating the table.
    type: str

text:
    default: false
    description: Treat all values in CSV/TSV as text.
    type: bool

alter:
    default: false
    description: If set, missing columns will be added automatically.
    type: bool

lines:
    default: false
    description: Indicates if the JSON file has one record per line.
    type: bool

table:
    description: Name of the table to insert data into.
    required: true
    type: str

ignore:
    default: false
    description: If a record with the same primary key already exists, the insertion will
      be ignored.
    type: bool

silent:
    default: false
    description: Do not show progress bar or any other output.
    type: bool

upsert:
    default: false
    description: If set, the existing rows will be updated.
    type: bool

analyze:
    default: false
    description: Analyze the table after inserting data.
    type: bool

convert:
    description: Dictionary specifying SQL conversion functions to apply to data during
      insertion.
    type: dict

db_path:
    description: Path to the SQLite database.
    required: true
    type: str

flatten:
    default: false
    description: Flatten nested structures in JSON/CSV into individual rows.
    type: bool

imports:
    description: Python modules to import when using --convert.
    elements: raw
    type: list

replace:
    default: false
    description: If a record with the same primary key already exists, it will be replaced.
    type: bool

not_null:
    description: Specifies columns that should be NOT NULL.
    elements: raw
    type: list

truncate:
    default: false
    description: If set, the table will be truncated before inserting the new data.
    type: bool

file_path:
    description: Path to the file containing the data to insert.
    required: true
    type: str

batch_size:
    default: 100
    description: Batch size for bulk inserts.
    type: int

empty_null:
    default: false
    description: Convert empty strings to NULL.
    type: bool

stop_after:
    description: Stop after inserting this many rows.
    type: int