How to append data from csv file to sqlite? - sqlite

I'm trying to add rows from a CSV file to an existing table.
In Mac Terminal, I start sqlite3:
sqlite> .open database.db
sqlite> .import test.csv table
.
.
.
test.csv:43200: expected 22 columns but found 1 - filling the rest with NULL
sqlite>
Notice there are 43200 rows in the csv file. The file itself has 22 comma separated columns, but sqlite only thinks there is 1 column.

According to the finds of Cgaters and the official documentation of SQLite:
Use the ".import" command to import CSV (comma separated value) data into an SQLite table. The ".import" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.
Note that it is important to set the "mode" to "csv" before running the ".import" command. This is necessary to prevent the command-line shell from trying to interpret the input file text as some other format.
Example:
sqlite> .mode csv
sqlite> .import C:/work/somedata.csv tab1

Related

Convert csv to db in sqlite

I have a csv file with ; separator. I would like to convert it into .db. I follow advice here: https://www.sqlite.org/cli.html#section_8
In my Ubuntu terminal I use:
sqlite3 ex1
Then I try:
.mode list
.separator ";"
.import odberatel.csv odberatel
.save odberatel.db
But When I look on the odberatel.db in Db Browser for SQLite it seems that is not separated correctly:
What do I do wrong, please?
From the same sqlite doc:
Note that it is important to set the "mode" to "csv" before running the ".import" command.

Strange display when output to a file in sqlite

I have a database named test.db with a table named test which contain two lines of data:
1 aa
2 bb
Then I write a script named test.sql:
.output result.txt
select * from test
On sysyem console, after running sqlite3 test.db < test.sql, the content of result.txt is:
1|aa2|bb
Why are all data in a single line? And what I want is:
1|aa
2|bb

SQLite importing csv file

I am using SQLite and I can't import this csv file for some reason. I ran cmd first then entered SQLite.
my input in cmd:
cd C:\Users\Fries\Desktop\SQLite
sqlite3
.mode csv
.import C:\Users\Fries\Desktop\city.csv cities
.schema cities
I am using this tutorial. http://www.sqlitetutorial.net/sqlite-import-csv/
for some reason I can't open the csv
I am new to dbms and sqlite seems to be a super simple one
Going by the feedback in the error message, it looks like the backslashes need to be escaped. I would first try this:
sqlite> .import C:/Users/Fries/Desktop/city.csv
The following might also work, using escaped backslashes:
sqlite> .import C:\\Users\\Fries\\Desktop\\city.csv

Loading multiple CSV files with SQLite

I'm using SQLite, and I need to load hundreds of CSV files into one table. I didn't manage to find such a thing in the web. Is it possible?
Please note that in the beginning i used Oracle, but since Oracle have a 1000 columns limitation per table, and my CSV files have more than 1500 columns each one, i had to find another solution. I wan't to try SQLite, since i can install it fast and easily.
These CSV files have been supplied with such as amount of columns and i can't change or split them (nevermind why).
Please advise.
I ran into a similar problem and the comments to your question actually gave me the answer that finally worked for me
Step 1: merge the multiple csv's into a single file. Exclude the header for most of them but write down the header from one of them in the beginning.
Step 2: Load the single merged csv into SQLite.
For step 1 I used:
$ head -1 one.csv > all_combined.csv
$ tail -n +2 -q *.csv >> all_combined.csv
The first command writes only the first line of the csv file (you can choose whichever one file), the second command writes the whole document starting from line 2 and therefore excluding the header. The -q option makes sure that tail never writes the file name as a header.
Make sure to put all_combined.csv in a separate folder, or in some distributions, it will be included recursively!
To load into SQLite (Step 2) the answer given by Hot Licks worked for me:
sqlite> .mode csv
sqlite> .import all_combined.csv my_new_table
This assumes that my_new_table hasn't been created. Alternatively you can create beforehand and then load, but in that case exclude the header from Step 1.
http://www.sqlite.org/cli.html --
Use the ".import" command to import CSV (comma separated value) data into an SQLite table. The ".import" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.
Note that it is important to set the "mode" to "csv" before running the ".import" command. This is necessary to prevent the command-line shell from trying to interpret the input file text as some other format.
sqlite> .mode csv
sqlite> .import C:/work/somedata.csv tab1
There are two cases to consider: (1) Table "tab1" does not previously exist and (2) table "tab1" does already exist.
In the first case, when the table does not previously exist, the table is automatically created and the content of the first row of the input CSV file is used to determine the name of all the columns in the table. In other words, if the table does not previously exist, the first row of the CSV file is interpreted to be column names and the actual data starts on the second row of the CSV file.
For the second case, when the table already exists, every row of the CSV file, including the first row, is assumed to be actual content. If the CSV file contains an initial row of column labels, that row will be read as data and inserted into the table. To avoid this, make sure that table does not previously exist.
Note that you need to make sure that the files DO NOT have an initial line defining the field names. And, for "hundreds" of files you will probably want to prepare a script rather than typing in each file individually.
I didn't find a nicer way to solve this so I used find along with xargs to avoid creating a huge intermediate .csv file:
find . -type f -name '*.csv' | xargs -I% sqlite3 database.db ".mode csv" ".import % new_table" ".exit"
find prints out the file names and the -I% parameter to xargs causes the command after it to be run once for each line, with % replaced by a name of a csv file.
You can use DB Browser for SQLite to do this pretty easily.
File > Import > Table from CSV file... and then select all the files to open them together into a single table.
I just tested this out with a dozen CSV files and got a single 1 GB table from them without any work. As long as they have the same schema, DB Browser is able to put them together. You'll want to keep the 'Column Names in first line' option checked.

Importing CSV file in SQLITE3 results in a blinking cursor with no action!

I ran the following command in SQLITE3 command line tool.
sqlite> .import out.txt Test
And the following is the result:
sqlite> .import out.txt Test
...>
"...>" keeps showing up if I hit enter. It looks like it's expecting another parameter, except I can't find anything on Google.
Thanks always
By default, sqlite3's .import expects tab-delimited data. You can change it with .separator.
An example:
$ cat >data.txt
1,2,3
$ sqlite3
SQLite version 3.5.1
Enter ".help" for instructions
sqlite> create table t(a integer, b integer, c integer);
sqlite> .separator ,
sqlite> .import data.txt t
sqlite> select a from t;
1

Resources