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'
Related
I have a big myfile.csv file that looks like this
From this file I want to import some columns and obivously rows and its relevant data into mysql database phpmyadmin.
This file is located on my local computer, I want to import some columns,rows with data from that file to my live database.
Here is what I have tried after searching google.
I created a table with following columns
id name email
Then tried to run the following query in my live database
LOAD DATA LOCAL INFILE '/tmp/myfile.csv' INTO TABLE registration_no
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
(#col1,#col2,#col3) set id=#col1,name=#col2,email =#col3
Note
myfile.csv is located on my computer, in C: drive.
Am I running the correct query,is the path /tmp/myfile.csv is correct ?
Query runs but the data isn't loaded into my live DB, please help me, I've spent one and half day figuring this out .
I have read this.
Step 1: (preferred) Try to have only the columns(csv file) which to be imported into DB.
ex: If 3 columns to be imported then in your myfile.csv remove other unnecessary columns
Step 2: Since you are using a windows system make sure the path is specified properly when loading the file.
Step 3: If your csv has headers to skip it use IGNORE 1 LINES.
So, the query would be like below.
LOAD DATA LOCAL INFILE 'C:/myfile.csv' INTO TABLE registration_no
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(id, name, email);
Step 4: (optional) If you need to import only specific columns from csv.
ex: csv contains 5 columns like id, name, reg_no, dob, email, but need to import only 3 columns id, name, email. Just insert the column into a non-existing variable.
Then the query should be like
LOAD DATA LOCAL INFILE 'C:/myfile.csv' INTO TABLE registration_no
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES #to remove first `n` rows usually used to remove header of a file.
(id, name, #dummy, #dummy, email);
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.
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.
Error: This query is just inserting the 1 line of file
LOAD DATA INFILE 'file.csv' INTO TABLE coords FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
And the file has 300k lines.
Columns are separeted by commas.
How is the coords created? Does it have 10 attributes for the data?
I just ran a quick test with a file containing six identical lines (using your data) and entered:
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE coord FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
It read all 6 lines in.
I did this under Linux. If you're on Windows maybe you need '\r\n' (?).
I'm trying to import a table from CSV file to phpMyAdmin.
This is my table:
1 |Evan|Chigur |Male|1987-05-25|codefiscale0123|Via Calatafini 17, Palermo, 23451|user#user.it|marco_r|ee11cbb19052e40b07aac0ca060c23ee
22 |Hank|Zappasorca|Male|1987-05-25|codefiscale0123|Via Calatafini 17, Palermo, 23451|user#user.it|marco_r|ee11cbb19052e40b07aac0ca060c23ee
When i try to fill my table with this, the first field is omitted.
I've already checked out the correspondences of the fields between the cvs and mysql table.
Can anyone help me?
i use this query:
LOAD DATA LOCAL INFILE 'C:\\wamp\\tmp\\php7E3C.tmp' REPLACE INTO TABLE `bw_users`FIELDS TERMINATED BY '|'ENCLOSED BY '"'ESCAPED BY '\\'LINES TERMINATED BY '*'
how does this work for you?
LOAD DATA INFILE 'C:\\wamp\\tmp\\php7E3C.tmp' REPLACE INTO TABLE bw_users
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
for windows change to
LINES TERMINATED BY '\r\n'