Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a tsv file with SQLite3

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?

like image 751
bsuire Avatar asked Sep 26 '14 18:09

bsuire


People also ask

How do I import data into SQLite?

First, from the menu choose tool menu item. Second, choose the database and table that you want to import data then click the Next button. Third, choose CSV as the data source type, choose the CSV file in the Input file field, and choose the ,(comma) option as the Field separator as shown in the picture below.

Which command is used to import CSV into sqlite3?

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.


2 Answers

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 
like image 62
adius Avatar answered Sep 28 '22 06:09

adius


You should create the table, set a separator and import the data sqlite wiki.

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 
like image 21
Grigorii Chudnov Avatar answered Sep 28 '22 06:09

Grigorii Chudnov