How to set multiple escape letters in MySQL 5.5 . When I set multiple letters ('\",\r,\015\n,\n,\b') in MySQL 5.0.22 it was ok , But in MySQL 5.5.28 is giving error code 'ERROR 1083 (42000): Field separator argument is not what is expected; check the manual'
My Query is as follows ,
select ID,MSISDN,IFNULL(TITLE,''),IFNULL(SECONDARY_TITLE,''),IFNULL (NAME,''),IFNULL(ACTIVATION_DATE,''),IFNULL(CREDIT_EXPIRY_DATE,''),
IFNULL(FIRST_VOICE_CALL_DATE,''),IFNULL(FIRST_SMS_DATE,''),IFNULL(FIRST_MMS_DATE,''),
IFNULL(FIRST_WAP_DATE,''),IFNULL(FIRST_TOPUP_DATE,''),IFNULL(TOTAL_USAGE,''),
IFNULL(TOTAL_TOPUP_AMOUNT,''),IFNULL(TOTAL_TOPUP_COUNT,''),IFNULL(LAST_TOPUP_DATE,''),
IFNULL(ACCOUNT_BALANCE,''),IFNULL(TARIFF_PLAN,''),IFNULL(DATE_OF_BIRTH,''),IFNULL(ADDRESS,''),
IFNULL(COUNTRY,''),IFNULL(CITY,''),IFNULL(POSTCODE,''),IFNULL(IC_NUMBER,''),
GENDER,IFNULL(FIXED_LINE_PHONE,''),IFNULL(EMAIL,''),IFNULL(PROFESSION,''),
IFNULL(EMPLOYER_NAME,''),IFNULL(EMPLOYER_ADDRESS,''),IFNULL(EMPLOYER_PHONE_NUMBER,''),
IFNULL(EMPLOYER_FAX_NUMBER,''),IFNULL(SIM_STATE,''),
IFNULL(ADDITIONAL_INFORMATION,'') INTO OUTFILE '/tmp/SUBSCRIBER_PROFILE.txt'
FIELDS ESCAPED BY '\",\\r,\\015\n,\\n,\\b' TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM SUBSCRIBER_PROFILE;
But When I set only one letter in 'escape by' option, it works
Please help me to solve this issue .
Thanks ,
Dhanushka
The escape character in your case should just be "\" because you're using \ to escape the character. It isn't asking for a list of sequences that you're going to escape, just the character that lets it know that it is being escaped, in this case the \.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Working with MySQL 5.0.21
mysql> select ID,MSISDN,IFNULL(TITLE,''),IFNULL(SECONDARY_TITLE,''),IFNULL(NAME,''),IFNULL(ACTIVATION_DATE,''),IFNULL(CREDIT_EXPIRY_DATE,''),
-> IFNULL(FIRST_VOICE_CALL_DATE,'') ,IFNULL(FIRST_SMS_DATE,''),IFNULL(FIRST_MMS_DATE,''),IFNULL(FIRST_WAP_DATE,''),
-> IFNULL(FIRST_TOPUP_DATE,''),IFNULL(TOTAL_USAGE,''),IFNULL(TOTAL_TOPUP_AMOUNT,''),IFNULL(TOTAL_TOPUP_COUNT,''),
-> IFNULL(LAST_TOPUP_DATE,''),IFNULL(ACCOUNT_BALANCE,''),IFNULL(TARIFF_PLAN,''), IFNULL(DATE_OF_BIRTH,''),IFNULL(ADDRESS,''),
-> IFNULL(COUNTRY,''),IFNULL(CITY,''),IFNULL(POSTCODE,''),IFNULL(IC_NUMBER,''), GENDER,IFNULL(FIXED_LINE_PHONE,''),IFNULL(EMAIL,''),
-> IFNULL(PROFESSION,''),IFNULL(EMPLOYER_NAME,''),IFNULL(EMPLOYER_ADDRESS,''), IFNULL(EMPLOYER_PHONE_NUMBER,''),
-> IFNULL(EMPLOYER_FAX_NUMBER,''),IFNULL(SIM_STATE,''),IFNULL(ADDITIONAL_INFORMATION,'') INTO OUTFILE '/tmp/SUBSCRIBER_PROFILE.txt'
-> ***FIELDS ESCAPED BY '\",\\r,\\015\n,\\n,\\b'*** TERMINATED BY '|' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM SUBSCRIBER_PROFILE;
*Query OK, 345812 rows affected (7.77 sec*)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Not working with MySQL 5.5.28
mysql> select ID,MSISDN,IFNULL(TITLE,''),IFNULL(SECONDARY_TITLE,''),IFNULL(NAME,''),IFNULL(ACTIVATION_DATE,''),IFNULL(CREDIT_EXPIRY_DATE,''),
-> IFNULL(FIRST_VOICE_CALL_DATE,'') ,IFNULL(FIRST_SMS_DATE,''),IFNULL(FIRST_MMS_DATE,''),IFNULL(FIRST_WAP_DATE,''),
-> IFNULL(FIRST_TOPUP_DATE,''),IFNULL(TOTAL_USAGE,''),IFNULL(TOTAL_TOPUP_AMOUNT,''),IFNULL(TOTAL_TOPUP_COUNT,''),
-> IFNULL(LAST_TOPUP_DATE,''),IFNULL(ACCOUNT_BALANCE,''),IFNULL(TARIFF_PLAN,''),IFNULL(DATE_OF_BIRTH,''),IFNULL(ADDRESS,''),
-> IFNULL(COUNTRY,''),IFNULL(CITY,''),IFNULL(POSTCODE,''),IFNULL(IC_NUMBER,''),GENDER,IFNULL(FIXED_LINE_PHONE,''),IFNULL(EMAIL,''),
-> IFNULL(PROFESSION,''),IFNULL(EMPLOYER_NAME,''),IFNULL(EMPLOYER_ADDRESS,''),IFNULL(EMPLOYER_PHONE_NUMBER,''),
-> IFNULL(EMPLOYER_FAX_NUMBER,''),IFNULL(SIM_STATE,''),IFNULL(ADDITIONAL_INFORMATION,'') INTO OUTFILE '/tmp/SUBSCRIBER_PROFILE.txt'
-> **FIELDS ESCAPED BY '\",\\r,\\015\n,\\n,\\b'** TERMINATED BY '|' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM SUBSCRIBER_PROFILE;
*ERROR 1083 (42000): Field separator argument is not what is expected; check the manual*
Is there a way to do something like the following ? which doesn't work but shows what I want to do
SET #OutputPath = '/Users/jo/Documents'
SET #fullOutputPath = CONCAT(#OutputPath,'/','filename.csv')
SET #fullOutputPath2 = CONCAT(#OutputPath,'/','filename2.csv')
SELECT * INTO OUTFILE #fullOutputPath
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
FROM database.tableName;
SELECT * INTO OUTFILE #fullOutputPath2
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
FROM database.tableName2;
Edit: Saving data(e.g. a table) into file without using variable (only constant values)
-- folder_path could could be like => c:/users/sami
-- choose the directory/folder already available in system
-- and make sure you have access to write the file there
SELECT * INTO OUTFILE 'folder_path/filename.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
FROM database.tableName;
Now using variable
Whenever you have to use a variable name in sql, you need dynamic sql (which is applicable in stored procedures only, neither in simple sql query nor in triggers or functions)
SET #OutputPath := 'Users/jo/Documents'; //or any folder_path
SET #fullOutputPath := CONCAT(#OutputPath,'/','filename.csv');
SET #fullOutputPath2 := CONCAT(#OutputPath,'/','filename2.csv');
set #q1 := concat("SELECT * INTO OUTFILE ",#fullOutputPath,
" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
FROM database.tableName");
set #q2 := concat("SELECT * INTO OUTFILE ",#fullOutputPath2,
" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
FROM database.tableName2");
prepare s1 from #q1;
execute s1;deallocate prepare s1;
prepare s1 from #q2;
execute s1;deallocate prepare s1;
As you had both ' and " in your query already, so I concatenated your query using " and used \ to escape your original " to ensure its use as a literal character and not used for concatenation
I just told the use of variable in sql. First You should make sure if your query works like example at the top (without using variable)
Conclusion: If your above query works fine then my told dynamic sql will work as well given that you are using it in some stored procedure.
I have a low carma so I'm posting an answer that should go as a comment to Sami's post - you need to enclose the file name by quotes (note added ' before and after #fullOutputPath):
set #q1 := concat("SELECT * INTO OUTFILE '",#fullOutputPath,
"' FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
FROM database.tableName");
If you want to do this from bash, i.e. export some data from mysql in csv to a file with dynamic name, it maybe easier and more readable like the following.
The SQL with embedded bash variables:
where (e.timestamp >= ${begin_ts} and e.timestamp < ${end_ts}) order by ed.timestamp ASC ) a
INTO OUTFILE '${export_path}' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
And the bash script that runs the sql file. Notice the envsubst command that evaluates the sql script and substitutes the variables.
#!/bin/bash
mysql_db="dbname"
mysql_user="mysqlpass"
mysql_pass="password"
export_path="./data.csv"
begin_ts="1478278490"
current_ts=$(date +%s -u)
sql=`export_path=${export_path} begin_ts=${last_ts} end_ts=${current_ts} envsubst < export.sql`
mysql $mysql_db -u $mysql_user -p$mysql_pass -e"${sql}"
You cannot do it in mysql CLI but in this way it works
mysql -e "SELECT * FROM database.tableName;" -u user -p database > filename.csv