Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 database - CSVREAD - skip loading header line of the csv file into db

Tags:

csv

h2

I am using H2 DB in my java application. I want to load a .csv file to the DB. This file contains column headers as the first line of the file. So while loading the file into the DB through CSVREAD command, H2 is trying to parse the first line also and thus failing.

So how to skip loading the first line. Below the query I am using to load the file to the DB:

"CREATE TABLE TEST (CIRCLE VARCHAR_IGNORECASE(50), MSISDN VARCHAR_IGNORECASE(50), PORT_IN_DATE TIMESTAMP, OPERATOR VARCHAR_IGNORECASE(255), PRODUCT_TYPE VARCHAR_IGNORECASE(255), PORT_ID VARCHAR_IGNORECASE(255)) AS SELECT * FROM CSVREAD('src/test/resources/test.csv', "
like image 590
Yatendra Avatar asked Dec 06 '22 17:12

Yatendra


1 Answers

The CSVREAD function supports both files with and without column header. If the file contains a column header, then don't supply the column list in the function, for example:

SELECT * FROM CSVREAD('test.csv');
SELECT * FROM CSVREAD('data/test.tsv', null, 'rowSeparator=' || CHAR(9));

and if the file doesn't contains the column header, then supply the column list in the function call, for example:

SELECT * FROM CSVREAD('test2.csv', 'ID|NAME', 'charset=UTF-8 fieldSeparator=|');
like image 194
Thomas Mueller Avatar answered Feb 06 '23 22:02

Thomas Mueller