2010-09-08 268 views
1

我想確定某個特定名稱的數據庫是否存在,然後如果它沒有創建它。理想情況下,這將在VBScript中。我目前正在嘗試使用數組的循環搜索,但數據庫的總數會發生很大變化。MySQL檢查數據庫是否存在

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 

回答

4

嘗試:

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

如果不存在,你會得到一個空的結果集。

如果你需要知道,如果一個數據庫存在試圖創造它時避免的錯誤:

CREATE DATABASE IF NOT EXISTS <name>; 
+0

模式給了一個錯誤,但該信息,如果不存在部分的創建數據庫是什麼,我要怎樣做。我一定錯過了。 – MrGrant 2010-09-08 22:45:20

0

,如果你能在一個MySQL命令行客戶端連接到服務器

show databases 

會告訴你在MySQL數據庫。

0

的最快方法是問題:

show databases; 
相關問題