【发布时间】:2012-03-06 22:21:43
【问题描述】:
我有几个 postgres 和 mysql 游标对象暴露给我,它们是在某个宇宙中创建的。如何从这些游标对象中找到数据库名称(以及有关该数据库的其他信息)?
cursor.__dict__ 没有提供任何有用的信息。
【问题讨论】:
标签: python mysql postgresql
我有几个 postgres 和 mysql 游标对象暴露给我,它们是在某个宇宙中创建的。如何从这些游标对象中找到数据库名称(以及有关该数据库的其他信息)?
cursor.__dict__ 没有提供任何有用的信息。
【问题讨论】:
标签: python mysql postgresql
如果你也有连接(称之为 conn):
conn.info.dbname
【讨论】:
对于 postgresql:-
cursor.execute("select current_database()")
db_name = cursor.fetchone()[0]
【讨论】:
我不了解 postgres,但使用 MySQLdb 你总是可以使用以下内容:
cursor.execute("select database()")
db_name = cursor.fetchone()[0]
也许有一种更清洁的方法可以做到这一点......
编辑:
对于其他信息,这取决于您要查找的内容,例如获取表名
cursor.execute("show tables")
for r in cursor.fetchall():
print r[0]
还有许多其他功能可用...有什么特别需要的吗?
【讨论】:
cursor_postgres.execute("select current_database()")