TransWikia.com

How can I use LOAD DATA LOCAL INFILE for specific columns?

Database Administrators Asked on November 5, 2021

I have a directory of text files, as the filename is the id, and the content text for this table:

CREATE TABLE texts
(
ID int(11) unsigned,
Added date,
Text MEDIUMTEXT,
PRIMARY KEY(ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci

I use PHP glob function to find files to INSERT file by file (row by row) as

INSERT INTO texts (ID,Added,Text) VALUES ($id,CURDATE(),'$text')";

$id is the filename, and $text the entire content of the file.

How can I use LOAD DATA LOCAL INFILE to directly INSERT into the database and to avoid reading the large text into PHP variable?

2 Answers

  1. CREATE a separate table for the load
  2. LOAD into that table
  3. UPDATE main JOIN temp ON ... SET MAIN... WHERE ... -- That is, transfer what you need to over into the real table from the temp table.

Answered by Rick James on November 5, 2021

Mysql can do following with user defined variables

 LOAD DATA LOCAL INFILE '/your_path/yourfule.csv'  
 INTO TABLE texts 
 FIELDS TERMINATED BY ','   
 LINES TERMINATED BY 'n'  
 ( @ID
 , @Text
 )
 SET `ID`   = @ID
   , `Added` = CURDATE()
   , `Text`     = @Text
 ;

Please check, if MySQL allows from that path to insert data.

And also you may have to change the options for INFILE

Answered by nbk on November 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP