martinwang1985
martinwang1985

Reputation: 556

using batch to detect whether database exists,if not ,create it

I am going to write a .bat file to realize this function: to detect whether the database and the table existed,if not exist,create them. I tried to write the bat file like this:

"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password= 
select * from martin.aaaperson

Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.

C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd

Upvotes: 0

Views: 3780

Answers (1)

Marc B
Marc B

Reputation: 360762

you need to do something like

mysql.exe -u root -p  < your_sql_commands_in_this_file.sql

Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.

Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

Upvotes: 1

Related Questions