Is it possible to export a CSV file with Doctrine? - mysql

Since there is no OUTFILE capabilities with Doctrine as far as I can tell, how would one export a query as a CSV file?
In MySQL, for example, the query would be:
SELECT * INTO OUTFILE 'file.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM table;
How could the same be done with Doctrine?

In pseudo code, I would to something like:
FETCH records in Doctrine_Core::FETCH_NUM mode
foreach records as record:
save record in csv using fputcsv
References: fputcsv

Related

Export JSON data from MySQL table to CSV

I used the following command to export some fields of MySQL table including a JSON field(attributes) into CSV file:
SELECT name, attributes, product_url FROM products INTO OUTFILE '/var/lib/mysql-files/toys.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
But, I get each key-value pair of attributes(JSON field) in separate columns.
How to get all those key-values(attributes column of MySQL table) in a single column of CSV file?
I found a solution that was enough to get my job done. I exported those fields into TSV instead of CSV using following slightly modified command:
SELECT name, attributes, product_url FROM products INTO OUTFILE '/var/lib/mysql-files/toys.tsv' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Still, if anyone has an exact solution to the problem, that would be greatly appreciated.

Mysql How to outfile in csv format, with sheets options?

I want to export data from mysql into a csv. There are many tables so I want a csv file with many sheets. How it can be done?
I suppose somethings like:
SELECT *
FROM product
WHERE active = 1
INTO OUTFILE '/root/tmp/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
SELECT *
FROM member
WHERE active = 1
INTO OUTFILE '/root/tmp/data.csv' // using the same .csv
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
etc...
You cannot append to an existing file using INTO OUTFILE.
https://dev.mysql.com/doc/refman/5.7/en/select-into.html says:
file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.
So you'll have to output to a different file per table, and then concatenate them together yourself (that is, not using SQL).

mysql - export table to csv file

I've created a PHP application that books in jobs. When an employee clicks the
EXPORT CSV button, I want a CSV file to download with all the SQL table information. How can I go about doing this.
I've seen some posts saying to run this query:
SELECT * FROM SalesOrders; OUTPUT TO 'c:\\test\\jobs.csv'
SELECT * INTO OUTFILE ''c:\\test\\jobs.csv''
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM SalesOrders;

Exporting an SQL table to CSV with commas escaped?

I have a large MySQL table (8-9k records, I'm using phpMyAdmin) with some values that are descriptions, with commas.
I need to Export the SQL table into CSV form to use on a new platform and I can't for the life of me solve this issue:
When there are commas in certain fields, the data gets shoved to the next column and things get out of order.
I JUST NEED the EXACT table in MySQL to become a CSV. Any tips, ideas?
Thanks.
Use this query:
SELECT * INTO OUTFILE 'filename.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM [tablename]
Source: http://www.wausita.com/2011/02/mysql-export-csv/
Simply go to the database's Export tab. Be sure to enclose your fields. This way values are wrapped in " to avoid erroneous commas.
Otherwise, you can use mysqldump from the command line.
Or you can export data with a query:
SELECT *
FROM table
INTO OUTFILE '/tmp/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
My solution was exporting as an .xlsx from MySQL. The columns were rendered perfectly. Using several combinations of enclosing/escaping character was to no avail.
I then saved the .xlsx as a .csv and it was fine.
Thanks for your time guys.

How to Export SQL Query to TXT Using Command Line

I want to export select * from table results to a text file from the command line in linux. How should I do this?
Thanks,
Jean
look at link
you only need to add this to the query
select * from table INTO OUTFILE '/tmp/myfilename.txt'
you can improve this to csv file (using it in excel latter)
like :
INTO OUTFILE '/tmp/myfilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'