I have a pre-defined table (with data) in a MySQL database with 3 columns:
brandId INT AUTO_INCREMENT,
brand CHAR,
insertDateTime DATETIME
And I have a list of brands stored in a csv file (10,000 rows).
I want to insert the brands into the table as new rows, with insertDateTime shows the date time of the insertion.
I know I can use LOAD DATA INFILE to load the brands from the csv, and I can use the NOW() function to compute insert datetime as we go, but how to combine them in one query?
You can use the SET clause of LOAD DATA to provide values that do not come from the input file.
Consider the following syntax:
LOAD DATA INFILE 'myfile.csv'
INTO TABLE mytable (brand)
SET insertDateTime = NOW();
I have a csv file (12 gb) which exported from oracle database,
formatted like
6436,,N,,,,,,,,,,,,04/01/1999,04/01/1999,352,1270,1270,406,406,1999,1,31/01/1999,0,88,0,A,11/12/2005,N,0,11/12/2005,,,,1270,1,0,,2974,,,,,,,,,,,,,,,,,,,,,,,,
As you see it has a lot of null values (mostly integer),
And when i import it to mysql database, It fills null values with zero
Like,
6436,0,0,,0,0,0,0,0,0,0,0,0,0,4,04/01/1999,04/01/1999,1270,1270,406,406,1999,1,31,31/01/1999,88,0,A,11/12/2005,N,0,11/12/2005,0,0,0,1270,1,0,,2974,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,00/00/0000,0,,0,0
What is the real issue here?
Thanks.
I figure it out with helpful comment of #Marc B
I wrote something like
LOAD DATA LOCAL INFILE '/path/data.csv' INTO TABLE table_name
FIELDS TERMINATED BY ','
IGNORE 1 LINES
(columns with #)
SET
column = IF(length(#column)= 0,null,#column),
date = str_to_date(#date, '%d/%m/%Y');
I have the following csv....
6353,test
7855,test2
I only want the second field to be imported. Ive tried the following for the specified column names in phpmyadmin
null, product_name
But no luck
You can simply write the columns you don't need into variables.
LOAD DATA INFILE 'file.csv'
INTO TABLE t1
(column1, #var1)
I upload data into a mysql table from csv file in a standard way like this:
TRUNCATE TABLE table_name;
load data local infile '/path/to/file/file_name.csv' into table table_name
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
(id, name, type, deleted);
All 'deleted' column entries in csv file has either 'current' or 'deleted' value.
Question: When csv data is being loaded into table, I want to put current date in table for all those corresponding 'deleted' entries in csv file. And null for 'current' entries. How can I do this?
Example: csv file:
id_1, name_1, type_1, current
id_2, name_1, type_2, deleted
id_3, name_3, type_3, current
Table after loading this data should look like this:
id_1, name_1, type_1, null
id_2, name_1, type_2, 2010-05-10
id_3, name_3, type_3, null
Edit Probably, I could run another separate query after loading csv file. Wondering if it could be done in same query?
You can use SET clause in LOAD DATA INFILE syntax:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
Using another query to change those deleted entries is quite simple. I'm going with this solution.
Couldn't you just load the CSV file into an array and then iterate through it? Or just iterate through the file itself?
Then you would just update/add the date to each record in your table as necessary (if field[2] == "deleted" or something like that).
I am trying to insert data into a mysql table from a csv file. I am using the infile sql command, but I am having trouble because the first column of the table is an id that is set as an auto increment field. what do I have to set my first column value to in order to get this to work, or can I do it at all?
Thanks
Try using an empty field or a TAB character as a first column value in a file
(see comment "Posted by Mohamed Abdulla on August 17 2005 11:14am" on http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html)
Another solution:
LOAD DATA INFILE 'DATA.txt' INTO TABLE your_table
FIELDS TERMINATED BY '^'
OPTIONALLY ENCLOSED BY '~'
(column2, column3, ... )
SET column1 = NULL