Importing .csv file to sqlite3 db table
Asked Answered
B

4

8

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

Boehike answered 9/12, 2014 at 5:3 Comment(8)
See the link at CSV ImportSalmagundi
thanks, but i referred to that link only for planning the script. it is not of much help in present situation.Boehike
Is the table already created before importing ? Can you show the content of testing.csv ?Salmagundi
yes, i created the table before importing data. i have attached the screenshot of testing.csv for your reference.Boehike
I think the command should be echo -e '.separator "," \n.import testing.csv aj_test' | sqlite3 ajtest.db because the table you are importing into is aj_test.Salmagundi
sorry I did a typo during posting the question. it is indeed aj_testBoehike
The script works on me. Can you show me the content of testing.csv which is the result of cat testing.csv.Salmagundi
echo -e 'attach database 'ajtest.db' as 'aj';\n.separator ","\n.import testing.csv aj_test' | sqlite3 ajtest.db This worked !!! I guess i needed to use an-- attach database ' ' as ' ' but why ???Boehike
B
9

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.

Boehike answered 15/12, 2014 at 9:1 Comment(0)
K
6

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.

Kozlowski answered 22/1, 2017 at 8:4 Comment(2)
i will definitely try with your solution and update. thanks.Boehike
(echo .mode csv; echo .import path/to/file.csv table_name) | sqlite3 filename.dbCroissant
A
5

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"
Ap answered 26/1, 2021 at 15:2 Comment(1)
This should be the accepted answer. Way more clean then the echo solutions.Francis
I
4

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!

Impasto answered 5/11, 2018 at 20:49 Comment(2)
I like this option, but it leaves me inside the sqlite shell.Ap
Thanks for your contribution... it led me to try this (which worked): sqlite3 -separator ',' stuff.db ".import myfile.csv t_table_name"Ap

© 2022 - 2024 — McMap. All rights reserved.