Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite import csv file with comma in text fields

I want to import csv file into SQLite db using

sqlite> .separator ,
sqlite> .mode csv data
sqlite> .import test.csv data

where data is the table name with three columns, just like the file.

The file has some string value that are encapsulated using double quotes. Some of the string values have commas in them (actual example from the file "Bond\, James") which should be treated as a single column, but SQLite produces an error

Error: test.csv line 2: expected 3 columns of data but found 4

How can I make SQLite import these values correctly?

like image 624
Tautvydas Avatar asked Mar 19 '14 15:03

Tautvydas


People also ask

How does CSV handle commas in data?

1. Enclose the text which has delimiters(comma) as a part of the text with double quotes/single quote. 2. When loading the source csv file, under the source tab, select Source Object > Formatting Options > Text qualifier > Double Quote / Single quote accordingly.

How do you load a CSV file with commas in the data?

Using the "From Text" feature in Excel Click the Data tab, then From Text. Select the CSV file that has the data clustered into one column. Select Delimited, then make sure the File Origin is Unicode UTF-8. Select Comma (this is Affinity's default list separator).

How do I import a CSV file into SQLite?

First, from the menu choose tool menu item. Second, choose the database and table that you want to import data then click the Next button. Third, choose CSV as the data source type, choose the CSV file in the Input file field, and choose the ,(comma) option as the Field separator as shown in the picture below.


1 Answers

I know this is a bit old, but this was the first relevant google search result, so I wanted to share my solution.

Use a different separator, and remove the quotes around values.

sed -i -e 's/","/|/g' -e 's/"$//g' -e 's/^"//g' file.csv

sqlite> .separator "|"
sqlite> .import file.csv tablename
like image 127
Gregory Avatar answered Oct 18 '22 23:10

Gregory