I want to import a whole CSV file (which was originally a CDR file and contains 108 fields) into a table of MySQL database with PHP 5. I use PDO.
In this line the import is made, but only the first line is loaded.
$AffectedRows = $pdo->exec("LOAD DATA LOCAL INFILE '".$csvfile."' INTO TABLE `sample` FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' ;");
The problem was caused by the file, it didn't have the extension (.csv) but i need the option IGNORE 1 LINES and it doesn't work! when i add it "AffectedRows = $pdo -> exec ("LOAD DATA LOCAL INFILE '".$csvfile."' INTO TABLE cdr_StandAloneCluster_01_201209111313_29945 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"); The table isn't charger any more!
Related
I use the INFILE function to copy a CSV file into a mysql database table, but I am not able to skip the first 2 lines automatically with IGNORE command. If I remove them in the CSV file it works and works also if I remove only the first line and I change the command as "IGNORE 1 LINES". Why I cannot skip the first two lines?
CSV file (data.csv) is as follow
Extraction Date: 2018-08-08
ID;name;surname;address;age
1;Alfred;Kennedy.Carbon Street;29
2;Mollie;Bush;First Avenue;22
3;Carl;Randolph;Second street;23
4;Anthony;Burger;Third Street; 33
The PHP code is as follow:
$query = "LOAD DATA LOCAL INFILE 'data.csv' INTO table table_data
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r'
IGNORE 2 LINES
(ID, name , surname, address, age)
";
mysql_query($query);
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 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 have to import a CSV file to MySQL from Yii. This is my query:
$sql = "LOAD DATA LOCAL INFILE '".addslashes($dir)."'
INTO TABLE `card`
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\n'
IGNORE 1 LINES
(`ID_CARD`,`SERIAL_NO`,`SERIAL_NO_CARD`,`ICCID`,`MSISDN`,`SITE_NAME`,`STATUS`,`DATE_HISTORY`)";
Yii::app()->db->createCommand($sql)->execute();
However, only column ID_CARD was inserted.
What am I doing wrong?
I already tested this on PHPMyAdmin but it did the same thing.
try this,
LOAD DATA LOCAL INFILE 'test.csv'
INTO TABLE card FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (ID_CARD,SERIAL_NO,SERIAL_NO_CARD,ICCID,MSISDN,SITE_NAME,STATUS,DATE_HISTORY)
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/