Determining the Current Database

9.15.1 Problem

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

9.15.2 Solution

Use the DATABASE( ) function.

9.15.3 Discussion

SELECT DATABASE( ) returns the name of the current database, or the empty string if no database has been selected. This Python code uses the statement to present a status display containing information about the current connection:

cursor = conn.cursor ( ) cursor.execute ("SELECT DATABASE( )") row = cursor.fetchone ( ) cursor.close if row == None or len (row) == 0 or row[0] == "": db = "(no database selected)" else: db = row[0] print "Current database:", db

Категории