How to use OUTFILE in codeigniter - mysql

I am trying to download the million of records from the database using OUTFILE. I am getting this error
Error Number: 1290
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
SELECT name, cp_id FROM campaign INTO OUTFILE '/var/www/html/test_csv/1184445179_180912015611.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ' ';
Filename: models/Admin_model.php
Line Number: 976
below is the model code:
$pth = rand().'_'.date('ymdhis').'.'.'csv';
$path = $_SERVER['DOCUMENT_ROOT'].'/test_csv/'.$pth;
$ad = $this->db->query("SELECT name, cp_id FROM campaign INTO OUTFILE '$path' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';")

Related

LINES TERMINATED BY '\n' not working when store mysl result in txt file

When I try to store mysql result in txt file lines terminated is not working
select * from users INTO OUTFILE '/var/lib/mysql-
files/users.txt'
FIELDS TERMINATED BY '|'
ENCLOSED BY '"'
LINES TERMINATED BY '\n';---> not working
the result are all in one line
"1"|"John#test.com"|"20"|"John"|"test"/"2"|"omar#test.com"|"21"|"Omar"|"test2""3"|"Mytest#test.com"|"30"|"Mytest"|"test3"

Adding current time to INTO OUTFILE 'C:/Output/data_.csv'

I am looking for a way to add the current time to my filename after every new export.
Current code:
SELECT * INTO OUTFILE 'C:/Output/data_.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM eloge_collector;
What i want to achieve:
SELECT * INTO OUTFILE 'C:/Output/data_2018-05-10-15-14.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM collector;
or something like this.
you can use prepared statements to create your query string then executing it
https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
this is my solution
first create create variable to hold the query string.
use concat function to inject current date time to the query string.
use DATE_FORMAT and now functions to get current date time
set #sql = concat("SELECT * INTO OUTFILE 'C:/filePrefix_",DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'),".fileExtension' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM `table`");
prepare s1 from #sql; --create statment from variable
execute s1; -- execute prepared statements

Backslash throws unexpected beginning of statement error

So I have this SQL Script for bulk loading my data:
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Bicycle');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Car');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Van');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('None');
SET foreign_key_checks = 0;
# Load data into categories
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CategoryData.txt'
REPLACE INTO TABLE CourierDB.Category
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(name)
SET categoryID = NULL; # Should trigger auto increment (or \r)
# Load data into package
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackageData.txt'
REPLACE INTO TABLE CourierDB.Package
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(price, itemName, category)
SET packageID = NULL; # Should trigger auto increment (or \r)
# Load data into address
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/AddressData.txt'
REPLACE INTO TABLE CourierDB.Address
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(buildingName, streetName, county, postcode)
SET addressID = NULL;
# Load data into packages
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackagesData.txt'
REPLACE INTO TABLE CourierDB.Packages
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(packagesID, package);
# Load data into branch
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/BranchData.txt'
REPLACE INTO TABLE CourierDB.Branch
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(branchName, address, headOfficeID, managerID, deliveryMethods)
SET branchID = NULL;
# Load data into consignment
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/ConsignmentData.txt'
REPLACE INTO TABLE CourierDB.Consignment
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(dispatchDate, consignmentType, branch, deliveryAddressID, returnAddressID, packages)
SET trackingID = NULL;
# Load data into DeliveryMethods
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/DeliveryMethodsData.txt'
REPLACE INTO TABLE CourierDB.DeliveryMethods
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(deliveryMethodID, deliveryMethod);
# Load data into Employee
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/EmployeeData.txt'
REPLACE INTO TABLE CourierDB.Employee
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(NIN, firstName, lastName, dateOfBirth, emailAddress, mobileNo, salary, branchID, supervisorID, address)
SET staffNo = NULL;
# Load data into CustomerConsignments
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CustomerConsignmentsData.txt'
REPLACE INTO TABLE CourierDB.CustomerConsignments
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(consignmentsID, consignment);
# Load data into Customer
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CustomerData.txt'
REPLACE INTO TABLE CourierDB.Customer
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(firstName, lastName, dateOfBirth, emailAddress, mobileNo, customerBranchID, address, consignments)
SET customerID = NULL;
SET foreign_key_checks = 1;
However with this said... I'm getting this error here for some reason when i put it into my relational schema:
Static analysis:
3 errors were found during analysis.
Unexpected character. (near "\" at position 0) Unexpected beginning of
statement. (near "\" at position 0) Unexpected beginning of statement.
(near "r" at position 1) SQL query:
\r LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackageData.txt'
REPLACE INTO TABLE CourierDB.Package FIELDS TERMINATED BY ',' LINES
STARTING BY '(' TERMINATED BY ')\r' (price, itemName, category) SET
packageID = NULL
MySQL said: Documentation
1064 - 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 '\r LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4' at line 1
Any help would be greatly appreciated! Thanks

MYSQL command string being cut

i have this code on my program
strSQL = "load data local infile 'C:/csv.csv' replace into table student_records fields terminated by ',' enclosed by '"' lines terminated by '\r\n'"
but the string it considers is only "load data local infile 'C:/csv.csv' replace into table student_records fields terminated by ',' enclosed by '"
and this is considered a comment' lines terminated by '\r\n'"
the string is being terminated by the double quote while it is still part of my mysql command.
can you give me another way around on how to do this?
i'm trying to automate the updating of data in my table.
Try escaping double quotes with "\" character:
strSQL = "load data local infile 'C:/csv.csv' replace into table student_records fields terminated by ',' enclosed by '\"' lines terminated by '\r\n'"

Filter and export mysql data by script

SELECT siirMisralari_txt
INTO OUTFILE 'C:/Users/Nikel/Desktop/mIRC_02/siir_07.txt'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '$crlf'
FROM `siirler`
WHERE `siirAdi_txt`
LIKE '%Ağustos Şiiri%'
this creates an empty file. why?