8

I'm trying to get the names of all databases associated with my MySQL server via python (2.7), but am instead just getting the number of databases.

I looked around, and it seems like the only answer may be to use sys for a command line call, get the name of the databases, and proceed from there, but I can't believe that's the only way.

Current Code:

import MySQLdb

serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")

c = serv.cursor()

print c.execute("SHOW DATABASES")

Output:

4

Thanks in advance for the help.

5
  • 3
    Try print c.fetchall() after you execute Commented Jul 3, 2017 at 20:39
  • 1
    @khelwood: Excellent, thank you! I'm obviously a noob at this. Also added l = c.fetchall(); l = [ i[0] for i in l ] as it was returning a tuple of tuples where the second item in the inner tuples was null - do you know why that would be? Commented Jul 3, 2017 at 20:42
  • Related: execute returns the number of rows in the result in your context. Commented Jul 3, 2017 at 20:45
  • 1
    Each tuple is a row in the query results. I don't specifically know why the tuple would have a null (or None) in it. Commented Jul 3, 2017 at 20:56
  • @khelwood - Fair enough, thanks again. Commented Jul 3, 2017 at 20:57

2 Answers 2

11

I am not sure if mysql connector is same as your library, but using msyql connector your answer would be something like this:

import mysql.connector
conn = mysql.connector.connect (user='user', password='password',
                               host='server_address',buffered=True)
cursor = conn.cursor()
databases = ("show databases")
cursor.execute(databases)
for (databases) in cursor:
     print databases[0]
Sign up to request clarification or add additional context in comments.

3 Comments

For python 3 use print(databases[0])
Hi, is there an alternative to the for loop for viewing the databases. I inspected the cursor object but didn't found any trace of the databases. Any help would be greatly appreciated .
If is not available mysql for python: pip install mysql-connector-python
1

Just taking previous comments and reorganicing: Current Code:

import MySQLdb
serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")
c = serv.cursor()
print c.execute("SHOW DATABASES")

With suggested modifications:

import MySQLdb
serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")
c = serv.cursor()
c.execute("SHOW DATABASES")
l = c.fetchall()
print l
l = [ i[0] for i in l ]
print l

In my case the output looks like (which is the desired output):

(('information_schema',), ('data',), ('mysql',), ('performance_schema',), ('sys',))
['information_schema', 'data', 'mysql', 'performance_schema', 'sys']

Please note the following common error to prevent mistakes: if you try c.execute("SHOW DATABASES").fetchall() you will have an error: AttributeError: 'int' object has no attribute 'fetchall'. This is way you should write firstly c.execute("SHOW DATABASES") and secondly c.fetchall().

3 Comments

if you get an error, this suggests that your code doesn't answer the question and provide a solution?
True, it works, the last paragraphs is to point out a common mistake. (at least for me)
Ok. I think you could clarify the wording or formatting to make that easier to understand.

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.