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

时间:2012-02-15 15:00:57

标签: python mysql postgresql

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

cursor.__dict__没有任何用处。

3 个答案:

答案 0 :(得分:3)

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

cursor.execute("select database()")
db_name = cursor.fetchone()[0]

也许有更简洁的方法来做这件事......

修改

对于其他信息,它取决于您正在寻找什么,但例如获取表名

cursor.execute("show tables")
for r in cursor.fetchall():
    print r[0]

还有许多其他功能......您有什么特别需要的吗?

答案 1 :(得分:1)

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

conn.info.dbname

答案 2 :(得分:0)

对于postgresql:-

cursor.execute("select current_database()")
db_name = cursor.fetchone()[0]