5

I'm troubleshooting a script I am using to query the database. To make sure I had everything working right I stripped it down to a simple 'SHOW TABLES' query. The problem is that it is returning a count of the tables instead of the list of names it should return.

import pymysql

connection = pymysql.connect(host='10.0.0.208', user='admin', passwd='Passwrd')

cursor = connection.cursor()
sqlstring = 'SHOW TABLES;'
cursor.execute('USE CustDB')
x = cursor.execute(sqlstring)

print(x)

This is only returning '17'. What am I missing??

1 Answer 1

4

Per the documentation, execute returns the number of rows affected

Returns: Number of affected rows

In order to get the desired results, you need to loop through the cursor

cursor.execute('USE CustDB')
tables = [c for c in cursor]

or use fetchall

cursor.execute('USE CustDB')
tables = cursor.fetchall()
Sign up to request clarification or add additional context in comments.

3 Comments

That did it. Thankyou
It was the fetchall() line I missed. I know I have done this a bunch of times before without the loop. fetchall() fixed it. Thankyou. Ill mark this as correct once the timer lets me
@Joe I'm glad I could be of assistance

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.