Recipe 9.11. Determining the Default Database


Problem

Has any database been selected as the default database? What is its name?

Solution

Use the DATABASE⁠(⁠ ⁠ ⁠) function.

Discussion

SELECT DATABASE⁠(⁠ ⁠ ⁠) returns the name of the default database or NULL if no database has been selected. The following Ruby code uses the statement to present a status display containing information about the current connection:

db = dbh.select_one("SELECT DATABASE()")[0] puts "Default database: " + (db.nil? ? "(no database selected)" : db) 

Note that before MySQL 4.1.1, DATABASE⁠(⁠ ⁠ ⁠) returns an empty string (not NULL) if there is no current database.




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net