8

I have a several postgres and mysql cursor objects exposed to me, created in some universe. How to find the database name (and other info about that db) from these cursor objects?

cursor.__dict__ gives nothing useful.

3 Answers 3

6

If you also have the connection (call it conn):

conn.info.dbname
Sign up to request clarification or add additional context in comments.

Comments

4

I don't know about postgres but using MySQLdb you could always use the following:

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

Maybe there's a cleaner way to do this...

Edit:

for other info it depends on what exactly you're looking for but for example to fetch table names

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

There are many other functions available... Is there anything specific you're looking for?

4 Comments

Neither of those will work if the cursor is attached to PostgreSQL.
Yes I know... As I said I don't know much about PostgreSQL. Could always use some things like 'SELECT table_name FROM information_schema.tables' I guess. But I don't know any other way to get this kind of information directly from the cursor :(.
@t00ny thanks for answer dude! I figured out the one for postgres similarly cursor_postgres.execute("select current_database()")
@jerrymouse I'm glad it's suitable for your needs!
2

for postgresql:-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.