Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: LOAD  (Read 7181 times)
Jose Edvandro M.
Posts: 6


« on: October 17, 2023, 01:55:46 pm »

I have a csv file, I'm trying to import it with the LOAD command. However, this file has a header. How do I import the file without importing the first line, which is the header?

* Lista CAT HETZ 04.10.23.csv (6.82 KB - downloaded 967 times.)
Reuben B.
Four Js
Posts: 1116


« Reply #1 on: October 19, 2023, 12:38:37 am »

With the LOAD we match the Informix-4gl LOAD statement which I don't believe has the ability to skip first row.  Having said I don't see what stops us adding syntax to ...

Code
  1. LOAD FROM filename [ DELIMITER delimiter]
  2. {
  3.   INSERT INTO table-specification [ ( column [,...]) ]
  4. |
  5.   insert-string
  6. }

... and adding a keyword similar to ...

Code
  1. LOAD FROM filename [ DELIMITER delimiter] [SKIP line]
  2. {
  3.   INSERT INTO table-specification [ ( column [,...]) ]
  4. |
  5.   insert-string
  6. }  

as that would not break compatibility.

However that does not help you now. Your current options might include ...

using tail to remove the first line, something like

Code
  1. tail -n + 2 filename > filename2
  2.  

or also suggested here https://stackoverflow.com/questions/28478657/informix-load-from-file-with-header load into a temp table which is all chars and then delete or ignore the first row before loading to destination table.


or you can explore writing your own generic library function using base.Channel, base.SqlHandle, base.StringTokenizer

Something like this as a starting point, need to add error handling, cater for case where delimiter is at end etc.

Code
  1. CALL generic_load("filename.unl", "INSERT INTO test_table VALUES(?,?)", "|", TRUE)
  2. ...
  3.  
  4. FUNCTION generic_load(filename STRING, sql STRING, delimiter STRING, omit_firstrow BOOLEAN)
  5.  
  6. DEFINE ch base.Channel
  7. DEFINE hdl base.SqlHandle
  8. DEFINE col_idx INTEGER
  9. DEFINE line STRING
  10. DEFINE tok base.StringTokenizer
  11.  
  12.    LET ch = base.Channel.create()
  13.    CALL ch.openFile(filename,"r")
  14.  
  15.    LET hdl = base.SqlHandle.create()
  16.    CALL hdl.prepare(sql)
  17.    BEGIN WORK
  18.    CALL hdl.open()
  19.  
  20.    IF omit_firstrow THEN
  21.        LET line = ch.readLine()
  22.    END IF
  23.    WHILE TRUE
  24.        LET line = ch.readLine()
  25.        IF ch.isEof() THEN
  26.            EXIT WHILE
  27.        END IF
  28.        LET tok = base.StringTokenizer.createExt(line, delimiter, "\\", TRUE)
  29.        LET col_idx = 0
  30.        WHILE tok.hasMoreTokens()
  31.            LET col_idx = col_idx + 1
  32.            CALL hdl.setParameter(col_idx, tok.nextToken())
  33.        END WHILE
  34.        CALL hdl.put()
  35.    END WHILE
  36.    CALL hdl.close()
  37.    COMMIT WORK
  38.  
  39.    CALL ch.close()
  40. END FUNCTION

(it would be nice if there was a method that combined ch.read and hdl.setParameter to avoid use of tokenizer (tokenizer doesn't have CSV as a delimiter ...)


Hope that helps,

Reuben







Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Michelle B.
Posts: 1


« Reply #2 on: January 15, 2024, 10:41:38 am »

I have a csv file, I'm trying to import it with the LOAD command. However, this file has asmash kartsheader. How do I import the file without importing the first line, which is the header?
If you are trying to import a CSV file with a header using the LOAD command, you can use the IGNORE keyword to skip the first line of the file. Here’s an example:

LOAD DATA INFILE 'filename.csv'
INTO TABLE tablename
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The IGNORE 1 ROWS statement tells MySQL to skip the first row of the file, which is the header. This will allow you to import the data from the CSV file without including the header row.
Clifton E.
Posts: 1


« Reply #3 on: February 15, 2024, 09:39:35 am »

If you are trying to import a CSV file with a header using the LOAD command, you can use the IGNORE keyword to skip the first line of the file. Here’s an example:
basket random
LOAD DATA INFILE 'filename.csv'
INTO TABLE tablename
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The IGNORE 1 ROWS statement tells MySQL to skip the first row of the file, which is the header. This will allow you to import the data from the CSV file without including the header row.
Many thanks for that solution.

Regard,
Clifton Evans
Jose Edvandro M.
Posts: 6


« Reply #4 on: February 21, 2024, 05:04:14 pm »

I have a csv file, I'm trying to import it with the LOAD command. However, this file has asmash kartsheader. How do I import the file without importing the first line, which is the header?
If you are trying to import a CSV file with a header using the LOAD command, you can use the IGNORE keyword to skip the first line of the file. Here’s an example:

LOAD DATA INFILE 'filename.csv'
INTO TABLE tablename
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The IGNORE 1 ROWS statement tells MySQL to skip the first row of the file, which is the header. This will allow you to import the data from the CSV file without including the header row.

LOAD DATA INFILE works only for mysql.
We work with postgresql, sql server, informix and oracle
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines