SQLite imports a csv comma file into text fields

I want to import a csv file into SQLite db using

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

where datais the name of a table with three columns, as is the file.

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

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

How can I get SQLite to import these values ​​correctly?

+4
source share
2 answers

I know this is a little outdated, but it was the first relevant Google search result, so I wanted to share my solution.

.

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

sqlite> .separator "|"
sqlite> .import file.csv tablename
+2

script, sql- csv.

csv sqlite3 , .

:

  • script sql
  • csv sql sqlite3
0

Source: https://habr.com/ru/post/1532500/


All Articles