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

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.

Related

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

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.

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;

Load Data Local Infile SQL

I have an extract of data that is separated by using this character |
I need to load this data into a MySQL database table but I'm struggling with the lines terminated by function.
Can anyone please help me identify the correct method to terminate these fields? My example data is below.
I've already tried '"|"' but this doesn't work for the first and last fields.
Example:
00000000|OLD TRUCK|9|13|02|Z |Z |9999|111|99|ZZ|ZZ|ZZ|ZZ||ZZ|ZZ|99|999|2
Try something like this:
LOAD DATA LOCAL INFILE 'example.txt' INTO TABLE example_table
FIELDS TERMINATED BY '|'
LINES TERMINATED BY ',';
This worked for me
LOAD DATA LOCAL INFILE 'd:/test.csv' INTO TABLE test
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n';

Import CSV data into SQL database

I have a table with the following fields:
Name (varchar)
Ranking (int)
Age (int)
Favourite Court (varchar)
and a CSV file like this
name;age;current_ranking;favourite_court
sample1;22;5;Hard
sample2;21;6;Clay
I try to import it with MySQL using: LOAD DATA LOCAL INFILE 'c:/players.csv' INTO TABLE players FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'; or LOAD DATA LOCAL INFILE 'c:/players.csv' INTO TABLE players FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' (name,age,current_ranking,favourite_court);, with or without file header (that is name;age...).
It does not work, fields are messed up. Why?
You need to name the columns from your table, not the columns as headed in the file:
LOAD DATA LOCAL INFILE 'c:/players.csv' INTO TABLE players
FIELDS TERMINATED BY ';'
IGNORE 1 LINES
(Name, Age, Ranking, `Favourite Court`)
Also, as noted under LOAD DATA INFILE Syntax:
NoteĀ 
If you have generated the text file on a Windows system, you might have to use LINES TERMINATED BY '\r\n' to read the file properly, because Windows programs typically use two characters as a line terminator. Some programs, such as WordPad, might use \r as a line terminator when writing files. To read such files, use LINES TERMINATED BY '\r'