thombashi / sqlitebiter

A CLI tool to convert CSV / Excel / HTML / JSON / Jupyter Notebook / LDJSON / LTSV / Markdown / SQLite / SSV / TSV / Google-Sheets to a SQLite database file.
https://sqlitebiter.rtfd.io/
MIT License
850 stars 50 forks source link

Is it possible to create auto increment - primary key column? #60

Closed maheshkumar2150 closed 5 years ago

maheshkumar2150 commented 5 years ago

Hi

How do i add auto increment primary column while converting json to sqlite. Thanks for this awesome plugin.

thombashi commented 5 years ago

Hi @maheshkumar2150

Did you mean adding primary key autoincrement column to the SQLite file while JSON does not include a key column? like the following example:

Input: issue.json

[
    {
        "a": "foo",
        "b": 123
    },
    {
        "a": "bar",
        "b": 456
    }
]

Output (current behavior)

'issue60' (a INTEGER, b INTEGER);
a b
foo 123
bar 456

Output (expected behavior)

'issue60' (id INTEGER PRIMARY KEY AUTOINCREMENT, INTEGER, b INTEGER);
id a b
1 foo 123
2 bar 456
maheshkumar2150 commented 5 years ago

Yes. Exactly, this is what i wanted to ask. Is it possible?

thombashi commented 5 years ago

Thank you for your reply.

You can add primary key id column as described in the previous comment with sqlitebiter 0.25.0 or newer version. I had added the feature to that version.

Please upgrade sqlitebiter and execute with --add-primary-key <PRIMARY KEY NAME> option like following:

Input: issue60.json

[
    {
        "a": "foo",
        "b": 123
    },
    {
        "a": "bar",
        "b": 456
    }
]

Example execution

$ sqlitebiter --add-primary-key id file issue60.json
[INFO] sqlitebiter file: convert 'issue60.json' to 'issue60' table
[INFO] sqlitebiter file: converted results: source=1, success=1, created-table=1
[INFO] sqlitebiter file: database path: out.sqlite

Output table schema: issue60

Field Type Null Key Default Index Extra
id INTEGER NO PRI NULL X
a TEXT NO NULL
b INTEGER NO NULL
maheshkumar2150 commented 5 years ago

Super! Thank you so much!!