Import a table without header

Hello,
I am learning how to import .csv file in SQLite and following the above link.
They show 2 separate cases, if you have already or don’t have yet the table in the database.
In the case where you have already the table and want to import the data, they show this import file_no_header command. I tried it, but SQLite don’t recognise it, and I ended up importing all and later deleting the first row.
Has any of you encountered this file_no_header command, and how does it work?
ps. I’m using the bash environment, not the studio.
Thanks.

ps. just for a laugh, the section “Your topic is similar to…” has very hilarious suggestion, like “3-dimensional Cube using just FOR loops in Java”. Maybe I should check there for an answer LoL

Hi @capitanf, I think you’re mis-reading the tutorial.

Since you’re working with sqlite directly via the command-line, importing a CSV is done by:

sqlite> .mode csv
sqlite> .import [path_to_file] [target_table]

where [path_to_file] and [target_table] are the path to your CSV file and the table you want to import into, respectively.

The tutorial you’re following says this:

If the table already exists, the sqlite3 tool uses all the rows, including the first row, in the CSV file as the actual data to import. Therefore, you should delete the first row of the CSV file.

The following commands import the city_without_header.csv file into the cities table.

sqlite> .mode csv
sqlite> .import c:/sqlite/city_no_header.csv cities

They are importing a different CSV file, called city_no_header.csv - not running a different command called file_no_header.

The SQLite CLI reference also says the following:

There are two cases to consider: (1) Table “tab1” does not previously exist and (2) table “tab1” does already exist.

In the first case, when the table does not previously exist, the table is automatically created and the content of the first row of the input CSV file is used to determine the name of all the columns in the table. In other words, if the table does not previously exist, the first row of the CSV file is interpreted to be column names and the actual data starts on the second row of the CSV file.

For the second case, when the table already exists, every row of the CSV file, including the first row, is assumed to be actual content. If the CSV file contains an initial row of column labels, that row will be read as data and inserted into the table. To avoid this, make sure that table does not previously exist.

So, to summarise - if you want to import the CSV data into an existing table you need to remove the “header” line, if one exists. But then, you already knew that…

:slight_smile:

3 Likes