Load Data Local Infile SQL - mysql

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';

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.

How to load data from csv to mysql

So the structure of the csv file is like this:
Country,City,AccentCity,Region,Population,Latitude,
ad,aixas,AixĂ s,06,,42.4833333,1.4666667
ad,aixirivali,Aixirivali,06,,42.4666667,1.5
ad,aixirivall,Aixirivall,06,,42.4666667,1.5
ad,aixirvall,Aixirvall,06,,42.4666667,1.5
The problem is that I don't know how to build the Load data query, I mean which separators to use and so on, mabe you guys can help me with an working example.
Does that help you?
load data local infile 'myfile.csv' into table myTable fields terminated by ','
lines terminated by '\n'
(Country,City,AccentCity,Region,Population,Latitude)

CSV import omit a field

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'

LOAD DATA LOCAL INFILE MySQL/PHP issue

Here is my MySQL table structure
id | tracking_number | order_id
Here is the structure of the CSV file:
(Sometimes the order_id is missing, and this seems to be causing issues)
"1R2689Y603406","33097"
"1R2689Y603404","33096"
"1R2689Y603414",
"1R2689Y603429","33093"
"1R2689Y603452",
Here is my current SQL Query which isn't working:
(The file is being uploaded, and is being read correctly, it's the query itself which is causing issues)
$sql = 'LOAD DATA LOCAL INFILE "'.$_FILES['my_file']['tmp_name'].'"
INTO TABLE table_tracking
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY "\""
LINES TERMINATED BY "\n"
(tracking_number,order_id)';
mysql_query($sql) or die(myqsl_error());
What is wrong with my query? Thanks for any help!
Edit: Changed CSV structure to represent missing data that sometimes occurs.
Changed query to match the one I am now using
Just trying here (not 100% confident) but did you try adding the destination columns like this:
mysql_query("LOAD DATA LOCAL INFILE '".$_FILES['my_file']['tmp_name']."'
INTO TABLE table_tracking FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(tracking_number,order_id)");
You have three columns in your table and are only providing two data.
This worked for me:
$sql = 'load data local infile "c:/users/ramon/desktop/1.csv"
into table test fields terminated by ","
optionally enclosed by "\""
lines terminated by "\n"';
mysql_query($sql) or die(myqsl_error());
You also probably need to make sure that your third column has a default value.
Instead of $_FILES['my_file']['tmp_name'], try
dirname(__FILE__)."/".$_FILES['my_file']['tmp_name']
dirname(__FILE__) will give the full directory path like 'C:/path/to/directory'.