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;
Related
when we import a csv by phpmyadmin using "csv using load data", we have this error in phpmyadmin and the import into our table from the csv is not done.
phpmyadmin version 4.4.15.10
With another version of phpmyadmin 4.4.15.8 with the same database and my same data server we have no problem.
thank you for your answers
Requête SQL :
LOAD DATA LOCAL INFILE '/tmp/phpRH1Q9u' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY ';' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\r\n'
MySQL a répondu: Documentation
#2000 - LOAD DATA LOCAL INFILE is forbidden, check mysqli.allow_local_infile
phpMyAdminWarning in ./libraries/dbi/DBIMysqli.class.php#262
mysqli_query(): LOAD DATA LOCAL INFILE forbidden
Backtrace
./libraries/dbi/DBIMysqli.class.php#262: mysqli_query(
object,
string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'',
integer 0,
)
./libraries/DatabaseInterface.class.php#183: PMA_DBI_Mysqli->realQuery(
string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'',
object,
integer 0,
)
./libraries/import.lib.php#170: PMA_DatabaseInterface->tryQuery(string 'LOAD DATA LOCAL INFILE \'/tmp/phpRH1Q9u\' INTO TABLE `glpi_corresp_usernames` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'')
./libraries/plugins/import/ImportLdi.class.php#158: PMA_importRunQuery()
./import.php#615: ImportLdi->doImport(array)
This helped my situation in phpMyAdmin:
run this command: show global variables like 'local_infile';
if value if OFF run: set global local_infile=true;
Then like #Guido Faecke said Check your php.ini for mysqli.allow_local_infile = On
in this line remove semicolon -> ;mysqli.allow_local_infile = On
restart apache and then you can run
LOAD DATA LOCAL INFILE...
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 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
I have a script that I am using to import .CSV data into a MySQL DB. The first field of data (email address) still has quote marks around it when I pull into MySQL. The other fields, the quotes are stripped out.
My CSV lines looks like this:
"email4#email.com"," Karla","Smith"
"email5#email.com"," Carl","Nichols"
The email addresses still have quotes in MySQL. The first and last name are fine.
Any suggestions?
<?php
$conn = mysql_connect('host','username','password');
mysql_select_db('db-name');
mysql_query("TRUNCATE TABLE contacts") or die(mysql_error());
mysql_query("LOAD DATA LOCAL INFILE 'New Member Weekly Report for Marketing.csv'
INTO TABLE contacts
Fields terminated by ',' ENCLOSED BY '\"'
LINES terminated by '\r'(
contact_email
,contact_first
,contact_last)")
or die("Import Error: " . mysql_error());
?>
On a little bit of googling, I found this:
Instead of writing a script to pull in information from a CSV file, you can link MYSQL directly to it and upload the information using the following SQL syntax.
To import an Excel file into MySQL, first export it as a CSV file. Remove the CSV headers from the generated CSV file along with empty data that Excel may have put at the end of the CSV file.
You can then import it into a MySQL table by running:
load data local infile 'uniq.csv' into table tblUniq fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(uniqName, uniqCity, uniqComments)
The fields here are the actual tblUniq table fields that the data needs to sit in. The enclosed by and lines terminated by are optional and can help if you have columns enclosed with double-quotes such as Excel exports, etc.
Source: http://www.tech-recipes.com/rx/2345/import_csv_file_directly_into_mysql/