Is it possible to tell Sqlite to ignore trailing field separator when using import command?

StackOverflow https://stackoverflow.com/questions/16925924

  •  31-05-2022
  •  | 
  •  

문제

I want to load a table with a text file delimited by pipes

Table
--------------------------

Id + Employee + Department

--------------------------

File

0|Some Name|Some department|
1|Another Name|Another department|

I use the .separator command to set the "|" separator and then execute the .import command.

I receive the following error Error: EMP.TXT line 1: expected 3 columns of data but found 4.

I want to know if ther is some option in sqlite3 to ignore the trailing pipe.

도움이 되었습니까?

해결책

You could import into a temporary table with a dummy column:

CREATE TEMP TABLE DummyTable(id, emp, dep, nothing);
.import importfile DummyTable
INSERT INTO MyTable SELECT id, emp, dep FROM DummyTable;
DROP TABLE DummyTable;

다른 팁

You could remove the superfluous pipe before importing:

< importfile sed 's/|$//' | sqlite3 my.db ".import /dev/stdin MyTable" 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top