55

I have a tsv (tab separated file) that I would like to import with sqlite3. Does someone know a clear way to do it?

I have installed sqlite3, but not created any database or tables yet.

I've tried the command

.import /path/filename.tsv my_new_table

but it gives me the error: no such table: my_new_table.

However, from what I'd read it should create the table automatically if it does't exist. Does it mean I need to create and use a database first, or is there another trick to importing a .tsv file into sqlite?

2 Answers 2

106

There is actually a dedicated mode for importing tab separated files:

sqlite> .mode tabs
sqlite> .import data.tsv people

Also if you include a header row in your tsv file, you can let sqlite automatically create the table. Just use an unused table-name during import and change the tsv file to:

name    param1  param2
Bob 30  1000
Wendy   20  900
3
  • 5
    don't put a semi-colon at the end of the import statement, like I did :) Aug 22, 2016 at 22:02
  • Error: no such table: people Feb 1, 2021 at 19:59
  • 1
    .help .mode says its job is to “set output mode,” but .help .import clarifies in a note: “If neither --csv or --ascii are used, the input mode is derived from the .mode output mode”
    – andrewdotn
    Jul 2, 2021 at 21:48
40

You should create the table, set a separator and import the data (sqlite docs).

Example for TSV:

data.tsv (tab as a separator):

Bob 30  1000
Wendy   20  900
  1. Create a table and set TAB as a separator:

    sqlite> create table people (name text, param1 int, param2 int);
    sqlite> .separator "\t"
    
  2. Import data:

    sqlite> .import data.tsv people
    

And the result is:

sqlite> select * from people;
Bob 30  1000
Wendy   20  900
3
  • 6
    Note that using .separator "\t" means that sqlite will still use csv-style rules for interpreting quotation marks, which probably isn't what you want. A better option is to use the tabs mode described in adius's answer.
    – Miles
    Jun 19, 2017 at 8:28
  • Seems like there is some logic on the names. After .separator "\t", I also had to rename from .csv to .tsv
    – Punnerud
    Aug 7, 2018 at 16:38
  • No need to create a table. The import will do it for you. If you are happy for every column's type to be chosen for you. I did this and everything was 'text'. This might be OK for situations where you are manually exploring data to throw away later. Aug 1, 2019 at 23:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.