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.
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 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 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!
I want to save my CSV File data to my database and using "Load Data Infile" as-
$sql="LOAD DATA INFILE '".$tempLoc."'
INTO TABLE `tbl_csv`
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(`mobile`, `name`, `email`) ";
I keep getting syntax errors for this query statement.
Error: CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 13 Can't get stat of '/tmp/phpikiYm6' (Errcode: 2). //tmp/phpikiYm6 is the file name
Sorry but I have no much reputations to comment on your question, however I cant see that you give the file extension to the query, have you tried:
$sql="LOAD DATA INFILE '".$tempLoc.".csv'
INTO TABLE tbl_csv
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(mobile, name, email) ";
Follow given checks to fix this problem :
perror 13
OS error code 13: Permission denied
Check these variables (using SHOW VARIABLES):
have_csv = YES
local_infile = ON
If those are correct, then...
The .csv file, I think, needs to be readable and locatable by the "mysql" user. Note that both "r" and "x" is needed on directories to let a user 'read' and 'search' the directory.
Suggest you change permissions on the file and directories of this part to 777:
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/