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"
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
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'"
This is my query:
SET #query2 = CONCAT('
SELECT * FROM table_name
INTO OUTFILE "',arg_file_path,'/',#var_table_name,'_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by ''"'' lines terminated by "\n" ');
But it produces following output:
SELECT * FROM lcs_tbl_test
INTO OUTFILE "/data/test_outfile/lcs_tbl_test_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by '"' lines terminated by "
"
Basically I want "\n" to print as it is in my prepared statement. It is executing \n as line separator in current code.
You need to use back slash as double means for '\' you need to use '\'. Please use below statement.
SET #query2 = CONCAT('
SELECT * FROM table_name
INTO OUTFILE "',arg_file_path,'/',#var_table_name,'_CURRENT_TIMESTAMP.csv" fields terminated by "," optionally enclosed by ''"'' lines terminated by "\\n" ');
Use \ as extra escape character to not interpret \n as new line character.
And, you may also require to change current_timestamp string from literal to dynamic value. Change your concat parameters as below:
SET #query2 = CONCAT(
'SELECT * FROM table_name INTO OUTFILE "',
arg_file_path, '/', #var_table_name, '_', ( CURRENT_TIMESTAMP + 0 ), '.csv"
fields terminated by ","
optionally enclosed by ''"''
lines terminated by "\\n" ');
It produces output as:
SELECT * FROM lcs_tbl_test
INTO OUTFILE "/data/test_outfile/lcs_tbl_test_20140410132736.csv"
fields terminated by ","
optionally enclosed by '"'
lines terminated by "\n"
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?