如何从postgres/mysql光标对象获取数据库名称
问题描述:
我有几个postgres和mysql光标对象暴露给我,在某些宇宙中创建。如何从这些游标对象中查找数据库名称(以及有关该数据库的其他信息)?如何从postgres/mysql光标对象获取数据库名称
cursor.__dict__
没有任何用处。
答
我不知道的Postgres,但使用MySQLdb的你总是可以使用以下方法:
cursor.execute("select database()")
db_name = cursor.fetchone()[0]
也许有一个更清洁的方式做到这一点...
编辑:
其他信息取决于你在找什么,但是例如拿到表名称
cursor.execute("show tables")
for r in cursor.fetchall():
print r[0]
还有很多其他功能可用...有什么具体的你要找的?
如果游标连接到PostgreSQL,那么这两个都不会起作用。 – 2012-02-15 15:49:35
是的,我知道......正如我所说我对PostgreSQL不太了解。我总是可以使用一些东西,如'SELECT table_name FROM information_schema.tables'。但我不知道任何其他方式直接从光标获得这种信息:(。 – t00ny 2012-02-15 15:54:31
@ t00ny谢谢你的答案哥们!我想出了一个postgres同样'cursor_postgres.execute(“select current_database()”) ' – jerrymouse 2012-02-15 19:16:58