如何从 postgres/mysql 游标对象获取数据库名称

jer*_*use 5 python mysql postgresql

我有几个 postgres 和 mysql 游标对象暴露给我,它们是在某个宇宙中创建的。如何从这些游标对象中找到数据库名称(以及有关该数据库的其他信息)?

cursor.__dict__ 没有任何用处。

Mic*_*ook 5

如果您也有连接(称为 conn):

conn.info.dbname
Run Code Online (Sandbox Code Playgroud)


t00*_*0ny 4

我不知道 postgres 但使用 MySQLdb 你总是可以使用以下内容:

cursor.execute("select database()")
db_name = cursor.fetchone()[0]
Run Code Online (Sandbox Code Playgroud)

也许有一个更干净的方法来做到这一点......

编辑:

对于其他信息,这取决于您到底要查找什么,例如获取表名称

cursor.execute("show tables")
for r in cursor.fetchall():
    print r[0]
Run Code Online (Sandbox Code Playgroud)

还有许多其他功能可用...您有什么特定的需求吗?