terça-feira, 30 de outubro de 2018

Importing CSV files to SQLite3 database


Today we will learn how to import n CSV files to a SQLite3 database. Yes, I wrote SQLite3, not SQLite, because the import command just has inside SQLite version 3.

First of all, you need install the SQLite3.
$ sudo apt-get install sqlite3

Now, execute the SQLite3.
$ sqlite3

This is the current version installed in my machine.
$ sqlite3
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>

Set the csv mode.
sqlite> .mode csv

Here I define the separator used in my CSV files.
sqlite> .separator ';'

Now I start importing the files.
sqlite> .import /home/lucio/desenv/file1.csv table1
sqlite> .import /home/lucio/desenv/file2.csv table2
sqlite> .import /home/lucio/desenv/file3.csv table3

Ok, I only imported 3 files, but you can import as many as you want.

You can check the tables created.
sqlite> .table
table1     table2     table3

Now, I save the tables in a sqlite file.
sqlite> .backup main my_database.db

And finish! I hope I was helpful.






Nenhum comentário:

Postar um comentário