Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV In To Temporary Table In MySQL

Tags:

mysql

I have a fairly sizable CSV file that can change from month-to-month, and has roughly 450 data points per row. I also need to manipulate the data before inserting it in to a permanent table. So my plan is to import it in to a temporary table, manipulate it, and then insert the data.

However, I can't find any information on if/how you import a CSV in to a temporary table (or, alternatively, some way of importing a dynamic CSV - the first row has column headers in the CSV).

I tried creating a temp table, with one column, and then importing the CSV but it doesn't seem to import it. This is what I tried so far:

DROP TEMPORARY TABLE IF EXISTS tmp_import;

CREATE TEMPORARY TABLE tmp_import (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY);

LOAD DATA LOCAL INFILE '/import.csv' INTO TABLE tmp_import
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;

As an example of the CSV, it's is in the format (all data points are numbers):

500,400,101,93,005,22
4,954,23434,123423432,44
like image 752
Ashley Avatar asked Nov 13 '15 10:11

Ashley


1 Answers

you could try the import option on command line

mysqlimport --ignore-lines=1 --fields-terminated-by=, --verbose --local
    -u [user] -p [database] /path/to/address.csv

/path/to/address.csv is the complete path to the CSV file, be sure to match the name of the file address in this case, with the name of the table, otherwish it will not work, the extension might be anything or none at all.

Or

LOAD DATA LOCAL INFILE 'File.csv' 
INTO TABLE tableName 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n'

reference: MySQL Documentation on LOAD DATA INFILE

like image 169
pipechaves Avatar answered Sep 28 '22 05:09

pipechaves