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.

Link to comment
Share on other sites

No sorry thats misleading, my appologies I can get my query to display on the screen ok so I can see the fields

 

Its just getting the information of that query from the screen into a txt file

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.