Export mySQL into CSV using OUTFILE - mysql

I've tried using PHP scripts found online with no success, so i reverted to using the mySQL Syntax OUTFILE, here's the current code i'm trying to use although it's throwing a few errors
SELECT Name, ID INTO OUTFILE 'data.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM data
It's throwing an error as shown below
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

Looks like this error is thrown by PHP. Try escaping the double quotes
SELECT Name, ID INTO OUTFILE 'data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM data
If this above query is working, then try escaping the backward slash again.
SELECT Name, ID INTO OUTFILE 'data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\\\\'
LINES TERMINATED BY '\n'
FROM data

Related

LibreOffice Base Macro run MySQL command ENCLOSED BY (and the enclosed are by qotation markes aka "

Im doing a macro in Libreoffice extract a txt file to mysql but all data has "double quotes".
in mysql terminal this command works fine.
LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES;
in macro like this it works oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' IGNORE 1 LINES;")
but if i add ENCLOSED BY '"' I get erro.
oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES;")
Thank you for your help
I found the solution
oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '" & """" & "' IGNORE 1 LINES;")

How to export a CSV file from the MySQL command line

I have a MySQL database containing a single table which is used as a temporary storage point for manipulating and querying data sets and is periodically deleted and replaced with new data. What I would like to be able to do is export the data from the MySQL command line to use for other purposes. I am using the XAMPP Apache server package with phpMyAdmin on Windows 10.
The issue I am having is the INTO OUTFILE syntax I am using returns an error relating to '\n'. Below is an example of the syntax:
SELECT *
FROM tablename
WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "''"
ESCAPED BY '\'
LINES TERMINATED BY '\n';
I have spent some time researching this without any luck, I have even tried using
LINES TERMINATED BY '\r\n'
but the error remained the same
ERROR: Unknown command '\''.
-> LINES TERMINATED BY '\r\n';
If anyone could provide any tips that would be greatly appreciated.
Use this
SELECT *
FROM tablename
#WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
I used some other syntax as you and deleted the OPTIONALLY which mysql doesn't like at that place
The problem is not in LINES TERMINATED BY but in ESCAPED BY.
This:
ESCAPED BY '\'
Is invalid syntax, because the backslash is interpreted as an escape character for the following quote. A decent text editor should let you see that.
You need to escape the backslash, like so:
ESCAPED BY '\\'
Alternatively, you can also use '\b':
ESCAPED BY '\b'
Another probem is that OPTIONALLY ENCLOSED accepts only a single character, while you are giving it two single quotes.
In your query:
SELECT * FROM tablename WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'" ESCAPED BY '\\'
LINES TERMINATED BY '\n'

How to export MySQL table into CSV without newlines in VARCHAR fields? [duplicate]

I am pretty inexperienced in SQL, so there should be a simple solution to my problem:
I am selecting a table into a comma-separated file, and the column of type TEXT has newline characters, so when I try to import my csv into Excel, it creates separate rows each piece of text following a newline character.
Here is my query:
SELECT * FROM `db`.`table` INTO OUTFILE 'c:\\result.txt' FIELDS TERMINATED BY ','
ESCAPED BY '\\' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ;
and then in Excel I import as a comma separated file which causes issues for column that has text with newline characters.
any help is appreciated!
Just enclose everything in double quotes perhaps.
SELECT * FROM db.table INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' ESCAPED BY '\\' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
Novikov is correct but you could also escape the new line characters while exporting.
SELECT REPLACE(`fieldname1`,'\n','\\n'),`fieldname2` FROM db.table INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' ESCAPED BY '\\' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
This will then replace all the new line characters with the text string '\n' This may not be what you want in the output though.
DC

Using MySQL OUTFILE with non-simple data

I've been using the following command to expert mysql data to a csv file.
SELECT * INTO OUTFILE 'output.csv' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM table1;
It works for simple tables with simple data. However, if the table contains html tags, double quotes, single quotes, ascii characters etc, it does not work propertly, i.e. it will put tabs and new lines in incorrect places, breaking up data where it shouldn't. How can the sql script above be improved to export data with html?
I have tried SELECT...INTO OUTFILE statement, and then LOAD DATA INFILE statement, everything is OK, the HTML text was exported/imported without any mistakes (on MySQL 5.5).
Try to add ENCLOSED BY option, it should help you, e.g. -
SELECT *
INTO OUTFILE 'output.csv'
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM
table1;
LOAD DATA INFILE 'output.csv'
INTO TABLE table1
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

PostgreSQL csv import from a MySQL csv export?

Will it work?
MySQL export:
SELECT * INTO OUTFILE 'C:/data.csv'
FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM table;
PostgreSQL:
COPY table FROM 'C:/data.csv' WITH DELIMITER AS '\t' NULL AS '\\N' CSV
There is missing columns for some reason. So I believe there is problem in delimiter. Am I correct, what can I do? I can inspect row with cause error below. But which characters I must look for?
ERROR: missing data for column "Column21"
CONTEXT: COPY table, line 88219: ...
As mentioned by #knitti postgres need to know escape character: ESCAPE '\'
OPTIONALLY ENCLOSED BY '"' is bad format for csv. It's better to force quoting.
Full code:
mysql 5.5.15:
SELECT *
INTO OUTFILE 'C:/data.csv'
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM table;
postgres 9.0.3:
COPY table FROM 'C:/data.csv' WITH DELIMITER AS E'\t' NULL AS '\\N' ESCAPE E'\\' CSV
Default quote character in PostgreSQL is double-quote, but the documentation says, you can use any single one-byte-character. Try adding
ESCAPE '\\'