I have a text file which contain 2 field sepearated by | and record by new line
example:
L'EQUME|7A
Voley|18
L'olivier|158
i have a MySql Table with 3 column (id, name , val)
//id autoincrement...
so i would like to use the mysql load file feature to insert the value into name and val but my main problem is the apostrophe while loading file ...
How to addslahes while querying via load file ?
LOAD DATA INFILE 'data.txt'
INTO TABLE table_name
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\r\n'
(name, val);
You can use escaped by But remember escaped by and enclosed by should not be same
I am assuming that your want to enter single quotes value and your fields are enclosed by double quotes and first line should be ignore.
try something like this
LOAD DATA INFILE 'data.txt'
INTO TABLE table_name
FIELDS
TERMINATED BY '|'
ENCLOSED BY '"'
ESCAPED BY ''
LINES
TERMINATED BY '\r\n'
IGNORE 1 LINES; //if you don't want to ignore first line than remove it
Related
I'm new here. I would like to ask question regarding with my codes. So my code is working perfectly but then after importing csv to mysql programmatically using LOAD INTO INFILE. I didn't know why my output kept getting this kind of format. Please see my codes below thanks!
$testing = $conn->prepare("LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
INTO TABLE trieinitialcountentry
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)");
$testing->execute();
And also, some rows had been imported perfectly but some of them are not. Example of my output:
Count_ID InvItemLocID OnHand OnHandValue StockCenter_ID
737450 -2091889269 140.00 "2 788.80"
Apparently some of the values in the input CSV file are wrapped in quotes and you didn't pass this information to LOAD DATA INFILE.
Try this query:
$query = <<<'END'
LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
INTO TABLE trieinitialcountentry
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)
END;
$testing = $conn->prepare($query);
$testing->execute();
Note the added OPTIONALLY ENCLOSED BY '"' clause.
I already written a query i have posted here but that is working with similar kinds of csv.I need a load data local infile query which will work with all kinds of csv file.Please look into it.
The below query works with all kinds of csv files in which fields separated by ,.
example csv
1,114300,1790,2,2,2,No,East
2,114200,2030,4,2,3,No,East
3,114800,1740,3,2,1,No,East
4,94700,1980,3,2,3,No,East
Query looks like this:
LOAD DATA LOCAL INFILE '$pathname'
IGNORE INTO TABLE $name
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES"
Another csv is like this
Agfa ePhoto 1280;1997;1024.0;640.0;0.0;38.0;114.0;70.0;40.0;4.0;420.0;95.0;179.0
Agfa ePhoto 1680;1998;1280.0;640.0;1.0;38.0;114.0;50.0;0.0;4.0;420.0;158.0;179.0
Agfa ePhoto CL18;2000;640.0;0.0;0.0;45.0;45.0;0.0;0.0;2.0;0.0;0.0;179.0
Agfa ePhoto CL30;1999;1152.0;640.0;0.0;35.0;35.0;0.0;0.0;4.0;0.0;0.0;269.0
Agfa ePhoto CL30 Clik!;1999;1152.0;640.0;0.0;43.0;43.0;50.0;0.0;40.0;300.0;128.0;1299.0
In the query i just changed Fields terminated by ',' to ';'.It works for me.I need everything to be done by in a single query.I am looking for help.
I extracted the csv filename and its path and stored it in a variable named $filename.
$file = new SplFileObject($filename);//it holds the name and location means path to csv
$file->seek(0);//It holds the first line of csv
if (strpos($file, ';') == true){
$field = "FIELDS TERMINATED BY ';'";
}
if (strpos($file, ',') == true){
$field = "FIELDS TERMINATED BY ','";
}
Then edit the query like this
LOAD DATA LOCAL INFILE '$pathname'
IGNORE INTO TABLE $name
$field
OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES"
It works for all the csv files which fields end by , and ;.
I have CSV file which has 3 columns, and my table has 4 columns(one of it is a id autoincrement which I want to be autoincremented). I want to load the 3 columns from csv to the selected 3 columns in the table.
Tried code:
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip (ipstart, ipend, countrycode) FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ;
which throws error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'' at line 1
The list of columns is in the wrong place in your statement. Follow the syntax in the documentation.
You want something like this:
LOAD DATA LOCAL INFILE 'info.csv'
INTO TABLE tbl_countryip
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(ipstart, ipend, countrycode)
Reference: http://dev.mysql.com/doc/refman/5.5/en/load-data.html
Follow below steps-
1) connect with any gui like sqlyog.
2) select db > select table > right click on table > choose import > import csv data using load...
3) select all columns except primary key.
4) put 1 in ingnore line option if your csv file contains header.
5) click on import
If you want to do by command then command should be as per below-
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (ipstart, ipend, countrycode);
Note: If your corrent dir not where csv file exist then you need to give full path.
If your csv file contains header line then you need to exclude it as per below-
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (ipstart, ipend, countrycode);
I removed the id column from the table, so both table and csv file had 3 columns, and then ran the load csv command, and then inserted a id column into the table with autoincrement.
I am uploading huge csv file in a mysql database using LOAD DATA local INFILE command.
The table has got single field(hids) only which is integer,primary and autoincrement.The query is
$insert_query = "LOAD DATA local INFILE '".$target_path."' INTO table master_huts(hids) OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES ";
The csv file contains,
ID
"343"
"454"
"777"
If I remove the double quotes from csv file then it works fine. How can I tell MySql to ignore double quotes from csv file.
Try this:
$insert_query = "LOAD DATA local INFILE '".$target_path."' INTO table master_huts(hids) FIELDS ENCLOSED BY '”' LINES TERMINATED BY '\n' IGNORE 1 LINES;
There is a similar question already but I would like to know how to accomplish this for very large data sets.
The #dummy method etc will be take very long.
Also, what should I do if the column names are different in the spreadsheet and the table?
LOAD DATA INFILE '/tmp/actor.tsv'
REPLACE INTO TABLE actor
FIELDS TERMINATED BY '\t'
OPTIONALLY ENCLOSED BY '"'
(#f1, #f2, #f3)
SET first_name = #f1, last_name = #f3
UPDATE. If number of fields is big use this script to generate the LOAD command:
echo -n "LOAD DATA INFILE '/tmp/actor.tsv'
REPLACE INTO TABLE actor
FIELDS TERMINATED BY '\\t'
OPTIONALLY ENCLOSED BY '\"'
("
comma=""
for i in `seq 300`
do
echo -n "$comma #f$i"
comma=","
done
echo ")"
echo "SET first_name = #f1, last_name = #f3"