user1804933
user1804933

Reputation: 453

Executing MySQL Queries using Python

I'm attempting to run some MySQL queries and output the results in my Python program. I've created this function that is called and the cursor is passed through. However, I am running into a problem where running the below code will always return None / nothing.

Here is what I have:

def showInformation(cursor):
    number_rows = 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "DB"'
    testing = cursor.execute(number_rows)

    print(testing)

When using the cursor object itself, I do not run into any problems:

for row in cursor:
         print(row)

Upvotes: 0

Views: 46

Answers (1)

JuniorCompressor
JuniorCompressor

Reputation: 20005

I guess you need:

print(cursor.fetchone())

because you are returning only a count and so you expect one row.

Calling execute is not supposed to return anything unless multi=True is specified according to mysql documentation. The programmer can only iterate the cursor like you did, or call fetchone to retrieve one row or call fetchall to retrieve all rows or call fetchmany to retrieve some rows.

Upvotes: 1

Related Questions