How to Export SQL Query to TXT Using Command Line - mysql

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'

Related

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;

Using this code to export mysql table to csv - but I need syntax for MAC not PC

Code for windows environment is below:
SELECT *
INTO OUTFILE '/documents/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM products
Just need the 2nd line for MAC environment - thanks.
First of all you need to put your FROM clause before INTO OUTFILE.
Second, be sure that the directory specified in OUTFILE has the ability for MySQL to write to it (in your case /documents). You'll have a much easier time writing to /tmp (MySQL already has permissions to write by default) than trying to change permissions on another directory to accept writes form your MySQL database.
SELECT *
FROM products
INTO OUTFILE '/tmp/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';

How to import xls file in to MySQL table

I'm using ubuntu 14.04 operating system and I want to import excel sheet(.xls) in to database.
I am using phpmyadmin mysql administration tool. If any one can give me the commands to import xls to mysql I would be grateful.
Import CSV file in to MySQL Table
Change file type in to CSV
Create table according to excel column names
Place the csv file in /var/lib/mysql
Open phpmyadmin sql command pane:
Execute this sql command
LOAD DATA INFILE 'testlist.csv'
INTO TABLE TEST
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Use the load data capability. See
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Export CSV file in to MySQL Table
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'testlist.csv'
FIELDS TERMINATED BY ','
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT * INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

Is it possible to export a CSV file with Doctrine?

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