Pratik
Pratik

Reputation: 109

How to check whether Database is available from command prompt

I an using mysql server. I want to check whether the 'X' database is available or not from the windows batch file, and if available i have to give message that Database is available. How can I do this, I tried many ways but did not succeed in any of them. Can anyone help?

Upvotes: 0

Views: 420

Answers (1)

bansi
bansi

Reputation: 57042

You can do it like the following in windows batch file.

@echo off
SET HAS_DB=0
FOR /F "tokens=*" %%a in (
    'c:\"Path to mysql install\bin\mysql" -uusername -ppassword -qNfsBe 
        "SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='databasename'"'
) do SET HAS_DB=%%a
IF %HAS_DB% == 1 (
    echo "Database is available"
) ELSE (
    echo "Database is not available"
)

This assumes MySQL username=username and password=password. Note there is no space between -u and username same with password.

Upvotes: 1

Related Questions