How to insert selected columns from a CSV file into one MySQL field using LOAD DATA INFILE [duplicate] - mysql

I have a file with some MySQL commands for loading a single column into a database table:
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(datapath);
The datapath file contains a list of files and their directories, i.e. x:\files\filename1.txt
All files are now in a single directory and I would like to hard-code the path into the command above, such that I can load only the filename but have the full path saved to the column in my table. I am aware that I could add an additional UPDATE statement above to update every column in the table after I've loaded it but that doesn't seem very efficient.
Does the LOAD DATA INFILE command support concatenating fixed strings with data being read in from a file?

TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(col1, col2, #my_variable)
SET col3 = CONCAT('x:\files\', #my_variable);

http://dev.mysql.com/doc/refman/5.6/en/load-data.html
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
...
[(col_name_or_user_var,...)]
[ SET col_name = expr,...]
When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. Then the resulting row is inserted into the table.

Related

load csv file in mysql table using LOAD DATA LOCAL INFILE command

I want to load csv file into mysql table.The query works fine but in csv row is like :
1000002,Kabul,"Kabul,Afghanistan",2004,AF,City,Active
Kabul and Afghanistan goes into 2 separate columns.below is my query:
LOAD DATA LOCAL INFILE "'.$file.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\'
LINES TERMINATED BY \'\n\'
I want "Kabul,Afghanistan" in one column.
The problem is that you need to add
OPTIONALLY ENCLOSED BY '"'
so that the loader knows to treat the quoted string as a single field.

mysql load data infile it contain more data than there were input column

I'm a new to mysql, I try load csv file to mysql.
the csv like:
1,"a,b"
2,bc
3,d
the table like this:
create table test(ind varchar(10),var varchar(20));
when I load this csv file:
load data infile 'test.csv' into table test
fields terminated by ',' ;
I change this
the warning:
row 1 was truncated: it contained more data than there were input columns
I try this:
load data infile 'test.csv' into table test
fields terminated by ','
optionally enclosed by '"'
it doesn't work.
the common of "a,b" cause this error. but I don't know how to solve this question.
It sounds like maybe LOAD DATA isn't properly picking up on your line breaks. Try adding LINES TERMINATED BY ... to your call:
LOAD DATA INFILE 'test.csv' INTO TABLE test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' -- use '\n' if on Linux
(ind, var)
With the above call, MySQL should not view the comma inside the first quoted term "a,b" as being a field separator, but rather just part of the text of that column.

load data infile format general col type to numeric

I am importing a .csv into a MySql table using LOAD DATA INFILE and would like to find a way around columns containing formatting like "6.10111E+11" -- this should import as "610111447853" but is instead 610111000000. The table col is VARCHAR as sometimes there are letters and those are necessary. Formatting the .csv column as numeric before saving it in the shared location does not seem to work. Can I specify some form of "Set" on that column upon import?
here is my code:
$query = <<<eof
LOAD DATA LOCAL INFILE '/home/c/Documents/UTC/UTC.csv'
INTO TABLE UTC_import
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
eof;

How to load data as text file in mysql?

How can I load data as text file in "file" column in mysql table?
If my table is: id,name,file
I don't want to use text file to load data in my table, I want to store the actual .txt files in my table.
LOAD DATA INFILE 'file_name.txt'
INTO TABLE database.table_name
FIELDS TERMINATED BY '\t'
ENCLOSED BY ''
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
STARTING BY ''
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
LOAD DATA LOCAL INFILE '/tmp/foo.txt'
INTO TABLE foo COLUMNS TERMINATED BY '\t';

Can I concatenate a hard-coded string with a column in a MySQL LOAD DATA INFILE?

I have a file with some MySQL commands for loading a single column into a database table:
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(datapath);
The datapath file contains a list of files and their directories, i.e. x:\files\filename1.txt
All files are now in a single directory and I would like to hard-code the path into the command above, such that I can load only the filename but have the full path saved to the column in my table. I am aware that I could add an additional UPDATE statement above to update every column in the table after I've loaded it but that doesn't seem very efficient.
Does the LOAD DATA INFILE command support concatenating fixed strings with data being read in from a file?
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(col1, col2, #my_variable)
SET col3 = CONCAT('x:\files\', #my_variable);
http://dev.mysql.com/doc/refman/5.6/en/load-data.html
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
...
[(col_name_or_user_var,...)]
[ SET col_name = expr,...]
When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. Then the resulting row is inserted into the table.