0

I want to save the query I wrote in mysql console to a .sql file... for example :

"CREATE TABLE Employee(id...,name...,salary);"

I want to save the code instead of it's result. thanks

3
  • Take a look at this Commented Dec 26, 2017 at 4:25
  • How are you executing this query? On the terminal, through a shell script... How? Commented Dec 26, 2017 at 4:26
  • WAMP app---->mysql console Commented Dec 26, 2017 at 4:40

1 Answer 1

0

Pass the query in a variable and then execute it. This way, variable will hold the query and you'll be able to do whatever you want:

  1. execute it
  2. send it to a file
  3. any other operation...

query="CREATE TABLE Employee(id...,name...,salary)";

mysql -u root -h localhost -P 3306 -p -e "$query"

echo $query >> query.sql

This'll print the data as well as populate a file query.sql in your PWD. The above example is a part of a shell script.

Sign up to request clarification or add additional context in comments.

4 Comments

I just want to have .sql file that's written in it ,say,"CREATE DATABASE Company;"
But I want to save this file using the mysql console environment
Happy to help :) Please accept/upvote it if it serves what you were looking for.
To avoid printing anything, simply skip the execution part i.e. mysql -u root -h localhost -P 3306 -p -e ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.