Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing .csv file to sqlite3 db table

I wrote a single line shell script to import a .csv file to sqlite3 database table.

echo -e '.separator "," \n.import testing.csv aj_test' | sqlite3 ajtest.db

sqlite3 database = ajtest.db sqlite3 table in ajtest.db = new_test

the testing.csv has 3 columns, first one is int the rest two are texts; so accordingly the structure of new_test is also--

sqlite> .schema aj_test
CREATE TABLE aj_test(number integer not null,
first_name varchar(20) not null,
last_name varchar(20) not null);

when the script is run, it does not show any error, but it also does not import any data. any guidelines as to what I have missed ???

content of testing.csv

like image 656
RicoRicochet Avatar asked Dec 09 '14 05:12

RicoRicochet


Video Answer


4 Answers

After much studies and discussion, I found an answer that is working properly,

echo -e ".separator ","\n.import /home/aj/ora_exported.csv qt_exported2" | sqlite3 testdatabase.db

the main thing is, that I needed to include the path of the .csv file in the import statement.

like image 174
RicoRicochet Avatar answered Oct 17 '22 10:10

RicoRicochet


I found this to work:

(echo .separator ,; echo .import path/to/file.csv table_name) | sqlite3 filename.db

The accepted answer fails to work for me.

like image 43
btk Avatar answered Oct 17 '22 09:10

btk


Why don't you take advantage of Sqlite's built-in command-line options to load CVS file to Sqlite database table? I assume you are writing bash shell script to load CSV files data to SQLite table.

Have a look on bellow single line bash script:

#!/bin/bash

sqlite3  -separator "," -cmd ".import /path/to/test.csv aj_test" ajtest.db

With my limited knowladge, I can't give you any example to automatically logout sqlite cli after being load done on db!

like image 3
A. Bauani Avatar answered Oct 17 '22 10:10

A. Bauani


This worked best for my needs because it is straightforward (unlike the echo solutions) and it doesn't leave the sqlite shell open.

#!/bin/bash 

sqlite3 -separator ',' stuff.db ".import myfile.csv t_table_name"
like image 2
Jon Carlson Avatar answered Oct 17 '22 10:10

Jon Carlson