wireservice / csvkit

A suite of utilities for converting to and working with CSV, the king of tabular file formats.
https://csvkit.readthedocs.io
MIT License
6.03k stars 603 forks source link

CSV based DDL Command #1192

Closed ravigit94 closed 1 year ago

ravigit94 commented 1 year ago

Hello support community,

Could you please advise if I can generate a DDL command based on the header of a CSV file. I don't need to connect to any database or insert data.

For an example, I have a CSV (Product.csv):

Prod_ID, Prod_name, Prod_launch_date
---------------------------------------------
1              Mouse            01/01/2019
2              Keyboard        02/02/2020

The output should be:

CREATE TABLE PRODUCT ( PROD_ID NUMBER, PROD_NAME VARCHAR(20), PROD_LAUNCH_DATE DATE)

Thank you.

jpmckinney commented 1 year ago

https://csvkit.readthedocs.io/en/latest/scripts/csvsql.html

echo "Prod_ID,Prod_name,Prod_launch_date
1,Mouse,01/01/2019
2,Keyboard,02/02/2020" | csvsql
CREATE TABLE stdin (
    "Prod_ID" DECIMAL NOT NULL, 
    "Prod_name" VARCHAR NOT NULL, 
    "Prod_launch_date" DATE NOT NULL
);

See documentation to change SQL dialect, table name, etc.