user1094081
user1094081

Reputation:

Insert comma separated values into MySQL table field

I've a text file that contains many megabytes of comma separated values (about 10 Mb): I must insert these values in a mysql db. Any value must be stored in a different row of a specific field of a table. Any suggestion is appreciated (I can eventually use PHP if needed).

Upvotes: 0

Views: 2196

Answers (4)

Kapil Jain
Kapil Jain

Reputation: 188

I think you should create a table with same numbers of column CSV file have. Than you can run this query into mysql to load the CSV file data into created table

LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;

Upvotes: 0

ashishmohite
ashishmohite

Reputation: 1120

You can write a php script in which you can:

  1. Use explode method to get all the entries in an array format
  2. Then run a for/foreach loop over that array to add elements to database

Upvotes: 0

PbxMan
PbxMan

Reputation: 7623

For this tasks I use tools such us Talend. You can do all kind of imports from CSV, files excel, etc, up to 450 connectors. it's open source and there is also a paid version with more features. It's java but you don't need to code in java unless you want to do something the application is not capable of doing. You may need to invest 3/4 hours doing the tutorias but it's worth the hassle.

Good luck

Upvotes: 1

message
message

Reputation: 4603

Just use search bar, there is a lot of samples at stackoveflow. Main phases:

  1. Parse CSV file
  2. Initiate database connection through PDO
  3. Run for/foreach loop to populate database

Upvotes: 0

Related Questions