You can import a CSV file into SQLite table by using sqlite3 tool and .import command. This command accepts a file name, and a table name.

Here, file name is the file from where the data is fetched and the table name is the table where the data will be imported into. In the absence of the table, it will create the table automatically according to the data in CSV file.

CSV file to SQLite table

Let’s take an example where we import the content of a CSV file to a table that doesn’t exist currently. Let’s name it “EMPLOYEE”. It will create a table based on the data of CSV file.

  mode csv  

.import /Users/javatpoint1/Desktop/sqlite/student.csv EMPLOYEE  

    Note: .mode csv is used before .import to prevent the command-line utility from trying to interpret the input file text as some other format.

    Sqlite Import 1

    Now check if the table is created:

    Sqlite Import 2

    You can see that EMPLOYEE table is created. Now check the data in EMPLOYEE table:

      mode column  
    
    SELECT * FROM EMPLOYEE;  
      Sqlite Import 3

      Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *