Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import .gzip file into sqlite / Could I skip some column while importing?

I tried to play around with .import but it seems to limited with csv and delimited file. Is it possible to import gzip file ? or at least, pipe from command line ?

Also, could I skip some un-wanted column like mysql "LOAD DATA INFILE" ?

like image 808
Tg. Avatar asked Nov 28 '22 08:11

Tg.


2 Answers

If you don't want to use named pipes, you could also:

zcat $YOURFILE.gz | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"

If you need to modify stuff before import, you could use perl (or awk, sed, whatever) between the zcat and sqlite commands.

For example, if your file already uses the pipe character as a delimiter and you would like to import only columns 0 to 3 and 5 to 6:

zcat $YOURFILE.gz | perl -F'\|' -anle 'print join("|", @F[0..3,5..6])' | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"
like image 164
mivk Avatar answered Dec 05 '22 09:12

mivk


$ mkfifo tempfile
$ zcat my_records.csv.gz > tempfile

This works like magic!

Although the mkfifo does create temporary file, the size of this file is 0 byte. When running this command $ zcat my_records.csv.gz > tempfile, it will halt at the command prompt. This allows you to run

sqlite3> .import tempfile db_table

After sqlite3 finished importing the named pipe, zcat command will also finish running. You can then remove the named pipe.

$ rm -f tempfile
like image 25
Bhoom Suktitipat Avatar answered Dec 05 '22 09:12

Bhoom Suktitipat