Design a Python command-line tool that accepts a CSV file as input and stores its contents in an SQLite database. The tool should also support querying the database by column name and value, returning matching rows as a JSON object.
PLAINFORMAT
import argparse
import csv
import sqlite3
import json

def import_csv_to_db(csv_file, db_name):
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()

    with open(csv_file, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)

        table_name = "data"
        cur.execute(f"DROP TABLE IF EXISTS {table_name}")
        columns = ", ".join([f"{col} TEXT" for col in header])
        cur.execute(f"CREATE TABLE {table_name} ({columns})")

        for row in reader:
            values = ", ".join(["?" for _ in row])
            cur.execute(f"INSERT INTO {table_name} VALUES ({values})", row)

    conn.commit()
    conn.close()


def query_db(db_name, column, value):
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()

    table_name = "data"
    cur.execute(f"SELECT * FROM {table_name} WHERE {column}=?", (value,))
    rows = cur.fetchall()
    header = [desc[0] for desc in cur.description]

    result = [dict(zip(header, row)) for row in rows]
    return json.dumps(result, indent=2)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="CSV to SQLite importer")
    parser.add_argument("csv_file", help="Path to the CSV file")
    parser.add_argument("--db", default="data.db", help="SQLite database name")
    parser.add_argument("--query", nargs=2, metavar=("COLUMN", "VALUE"), help="Query the database by column and value")

    args = parser.parse_args()

    if not args.query:
        import_csv_to_db(args.csv_file, args.db)
        print(f"Imported {args.csv_file} to {args.db}")
    else:
        result = query_db(args.db, args.query[0], args.query[1])
        print(result)