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:
Related
I have a PHP script where I upload a CSV file and this one update/insert rows into mysql database, all is on ubuntu 18.04 server and this PHP script had many years working without problems, the server has more than 1 year working, suddenly a couple of weeks ago, with any change on server or software stop working.
The weird thing is it is not generating any error, no logs error, nothing, so i can't debug.
I read a lot this few days ago without any lucky, so i am not sure what to do.
On my tests and testing similar problems, i found this:
If i run the "Load data local infile" directly on the server it works
The file is uploading fine, i can give 777 permision and nothing changes
infile is enabled on mysql
apparmor is configured
user who connect to database has FILE on privileges
Here is the used code:
LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
As I said at the begining, this is no new server, this is no new code, has many years working, when debug I only received this on screen:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y') failed
0:
Where is "0:" it must to be the error code and after ":" the description of the error.
If I remove LOCAL i get this error:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
not found (Errcode: 13 - Permission denied)
29: File '/var/www/html/testing/unicomer/pagos/datos/pagos/pagos al 29.csv' not found (Errcode: 13 - Permission denied)
I did a test with a user with ALL THE PRIVILEGES and do not work, so I don't know if this is a mysql issue or server issue.
Thank you very much for your help.
I'm trying to load a CSV file into a table. I'm getting a permission denied error:
Error importing CSV File: (1045) Access denied for user 'username'#'localhost'
(using password: YES)
SQL=LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
( contact_lname, contact_email ) ;
The csv file exists, and has permissions set to 0644.
The SQL statement is:
LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(
contact_lname,
contact_email
);
I've tried using it with both a leading single slash and the double slash (as above) in the filename. I've also tried running it in phpAdmin and get the same error message.
The site is hosted on a Hostgator shared account.
I've added code to verify that the file exists, and has the right permissions, and it says it does. But MySQL doesn't seem to be able to access the file.
Yes, I know that Joomla 2.5 is end of life, but for many reasons, this site can't be upgraded, and is only used for admin functions.
Any suggestions would be welcome as the alternative is to read the file with PHP and insert each row which would be much slower.
The first 4 lines of the CSV:
"name","email"
"John Smith","xxx1#yyy.edu"
"John Doe","xxx2#yyy.edu"
"Jane Doe","xxx3#yyy.edu"
This error is usually due to you not including the LOCAL keyword. Try:
LOAD DATA LOCAL INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY ''
IGNORE 1 ROWS
(
contact_email,
contact_email
);
The permission error is misleading, it relates to trying to the mysql system directories rather than the file you are trying to read.
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'm fairly new to php and CakePHP, and I'm trying to execute the following code after uploading a text file (2kb):
$filefullname = $this->request->data['File']['file']['tmp_name'];
debug($filefullname);
move_uploaded_file($filefullname, WWW_ROOT.'tmp.txt');
$query = 'LOAD DATA LOW_PRIORITY INFILE "'.WWW_ROOT.'tmp.txt'.'" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES';
debug($query);
$this->Agency->query($query);
Though the file can be found, I get the following output:
'C:\Windows\Temp\phpB413.tmp'
'LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES'
Error: SQLSTATE[HY000]: General error: 29 File 'C:\Data\myphpapplication\app\webroot\tmp.txt' not found (Errcode: 13)
SQL Query: LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES
How can I fix this?
Most likely the file is there, but MySQL doesn't allow you to read files from that location. MySQL is fairly restrictive from where it reads LOAD DATA ... INFILE, with good reasons. You may need to move your file first to a path that MySQL can read from, or change the settings for your MySQL server.
From the MySQL 5.1 manual:
Note that, in the non-LOCAL case, these rules mean that a file named
as ./myfile.txt is read from the server's data directory, whereas the
file named as myfile.txt is read from the database directory of the
default database. [...]
And:
Windows path names are specified using forward slashes rather than
backslashes. If you do use backslashes, you must double them.
#JvO is guiding you down the right path. I think you're missing a couple of important elements, one of which is moving the uploaded file from its initial temp directory on the server to a place where MySQL can officially read it (it won't read it from a temp directory for security reasons).
Here's an example from a project I did, which uploaded a CSV file and then MOVED it to a readable directory. I first uploaded it with the move_uploaded_file function, and then created a variable describing the uploaded path of the new file for use in a LOAD DATA query.
move_uploaded_file($_FILES["fCSV"]["tmp_name"],"user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"]);
$csvfile = "user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"];
In your LOAD DATA query, you're missing the "LOCAL" keyword, which tells MySQL to read the file from a specified directory, otherwise it's looking for it in the database directory (the use of "Field1" etc. below is for demo purposes only and you should have your real table field names in there):
$sqlLoad = "LOAD DATA LOCAL INFILE '".$csvfile."' INTO TABLE my_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (Field1,Field2,Field3,Field4) SET ID =".$id
Lastly, you really need to read through this page in the MySQL manual. It covers many of the issues you're dealing with along with providing examples:
MySQL LOAD DATA INFILE Syntax
Following is my sql query which worked fine when i ran it from phpmyadmin but when i ran the same query through PHP then the query didnot worked and gave me following error. Kindly let me know how can I run this query from my php file so it will create the output file in the specified folder. Thanks,
QUERY
SELECT * FROM lahore_student INTO OUTFILE 'b://uploaded//data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Output: It creates file to my b drive in FOLDER named as Uploaded
ERROR:
File 'b:uploadeddata.csv' already exists
NOTE: The uploaded folder was empty
It looks you are trying to run this on a windows system, which requires special treatment of directory separators.
Try:
SELECT * FROM lahore_student INTO OUTFILE 'b:\\uploaded\\data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Or:
SELECT * FROM lahore_student INTO OUTFILE 'b:/uploaded/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Also note that this will not overwrite an existing file (which is the error you are getting). So you need to make sure that any file with that name is deleted first.
See the The “\” path name separator character section on this page in the MYSQL docs:
http://dev.mysql.com/doc/refman/5.0/en/limits-windows.html