tags:

views:

45

answers:

3

I'm trying to determine if a database of a certain name exists and then create it if it does not. Ideally this would be in VBScript. I currently am trying using a loop search of an array but the total number of databases will change greatly.

set dbQuery = ConnSQL.execute(checkDBsql) 
            if dbQuery.BOF and dbQuery.EOF then ' Query didn't return any records.
                ConnSQL.execute(MakeDb)
            else
                dbQuery.MoveFirst
                i = 0
                Do While Not dbQuery.EOF
                    i = i + 1
                loop
            end if
    set dbQuery = ConnSQL.execute(checkDBsql)
            if dbQuery.BOF and dbQuery.EOF then ' Query didn't return any records.
                msgBox "ERROR!"
            else
                e = 0
                do while not dbQuery.EOF
                    DBName(e) = dbQuery("Database")
                    e = e + 1
                loop                
                For a = 1 to UBound(DBName)
                    If DBName(a) = OldDBName Then
                        MsgBox DBName(a)
                    end if
                Next
    connSQL.close
A: 

if you can connect to the server in a mysql command line client

show databases

will show you the databases in mysql.

hvgotcodes
A: 

The quickest way is to issue:

show databases;
zevra0
+4  A: 

Try:

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'

You'll get an empty resultset if not exists.

If you need to know if a database exists to avoid an error when trying to create it:

CREATE DATABASE IF NOT EXISTS <name>;
p.campbell
The information Schema gave an error but the Create database if not exists part is what I was trying to do. I must have missed that.
MrGrant