Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

How to export the result of mysql cmd to a text file?

Does anyone know how to export the result of mysql cmd to a text file?
0
kennysflau
Asked:
kennysflau
  • 2
  • 2
1 Solution
 
Kim RyanIT ConsultantCommented:
Use the -e option to run a command and quit, and redirect output to a text file.

mysql -u user -ppasswd -D dbase_name -e 'select * from table_name'  > report.txt
0
 
ygouthamCommented:
there is a separate command in my sql called select into outfile

http://dev.mysql.com/doc/refman/5.0/en/select.html

make sure that the file already does not exist before giving the command

*********
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;
***********

in this instance the file "/tmp/result.txt" if it already exists then it gives a error.  it gives you a text file with , as the field separator to be imported into a excel, access, openoffice calc etc.
0
 
Kim RyanIT ConsultantCommented:
This only works if you want a select statement. The -e option above will work with any statement, update, insert etc
0
 
ygouthamCommented:
teraplane,  insert / update will only say how many number of rows were affected. more often users are looking for selecting values to a text file based on some suggestion.  and hence my suggestion.  

and  if the requirement is to run it as a scheduled task (in your instance you are presuming it to be alinux based system) whereas i only suggested a more generic sql syntax which is available as a pre-defined feature.

different ideas, no offence!!!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now