问题的背景是:
1。要循环检查多个数据库(数目不定),如果出错了,要提示相应的数据库的名字,但是函数调用有很多层,不想每层都传数据库名。mysql有没有系统变量,表示当前的数据库名?怎么在程序中得到?开发环境是VS2003 MFC 开发。

解决方案 »

  1.   

    DATABASE() mysql> SELECT DATABASE();
            -> 'test'DATABASE() 
    返回使用 utf8 字符集的默认(当前)数据库名。在存储程序里,默认数据库是同该程序向关联的数据库,但并不一定与调用语境的默认数据库相同。  mysql> SELECT DATABASE();        -> 'test'若没有默认数据库, DATABASE()返回 NULL。
      

  2.   

    DATABASE() Returns the default (current) database name as a string in the utf8 character set. If there is no default database, DATABASE() returns NULL. Within a stored routine, the default database is the database that the routine is associated with, which is not necessarily the same as the database that is the default in the calling context. mysql> SELECT DATABASE();
            -> 'test'If there is no default database, DATABASE() returns NULL. 
      

  3.   

    使用DATABASE(),可以返回当前数据库名称。如果没有打开数据库则返回null。