Jump to content

Write query to .txt file and update


adzie

Recommended Posts

ok let me try and make this clearer as i think my question was confusing.

 

What i want to do is extract certain fields of information from a DB. extract that into a txt file. every 24 hours create a cron which will delete the txt file and again extract the information adding any new entries this time

 

any help appreciated thanks.

Well, from the OS shell prompt, you can use the -e switch and redirect the output to a file with ">".  From inside the mysql client, you can use ...INTO OUTFILE (see manual for usage), but that method is more limited because, for one, you can only write to files that don't already exist (security issue).

 

What OS?

 

On either Win or *nix, typically:

$ mysql test -e "SELECT * FROM test_table"
+------+----------+------+
| id   | order_no | name |
+------+----------+------+
|    1 |       20 | bxxx |
|    2 |     NULL | bbbb |
|    3 |     NULL | cccc |
+------+----------+------+
$ mysql test -e "SELECT * FROM test_table" > output.txt
$

 

Except, in output.txt, the columns will be tab delimited unless you specify "--table" on the command line.

thanks for the help

 

i really need it to update the file so having it exist is there a way around it?

 

I also need the information in the following format in the text file

 

level name password

level name password

level name password

level name password

 

thanks

sorry didnt mean to offend It linux by the way I'll try that code

you put there

 

$ mysql test -e "SELECT * FROM test_table"

+------+----------+------+

| id  | order_no | name |

+------+----------+------+

|    1 |      20 | bxxx |

|    2 |    NULL | bbbb |

|    3 |    NULL | cccc |

+------+----------+------+

$ mysql test -e "SELECT * FROM test_table" > output.txt

 

Like I said at first, the following how to query and capture output from the prompt (the dollar sign, $, represents the Linux prompt):

 

$ mysql your_database < query_in_a_file.sql > output_file.txt

 

You can put a command to that effect in your crontab or wherever will accept a shell command.  You will need to use any user/password authentication that is normally required, just like using the mysql client interactively.

 

$ mysql your_database -u username -pxxxxxx < query.sql > output.txt

 

If your query is short and simple, maybe you don't want to keep it in a seperate file.  You can use the -e option to specify the query directly at the prompt:

 

$ mysql your_database -u username -pxxxxx -e "SELECT level,name,password FROM your_table" > output.txt

 

Btw, this is all in the man page for the mysql client.

 

$ man mysql

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.