MySQL "SHOW TABLES" Returns a score instead of a list (Python)

I am trying to troubleshoot a script that I use to query a database. To make sure everything is working correctly, I split it into a simple "SHOW TABLES" query. The problem is that it returns the number of tables instead of the list of names that 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 returns only "17". What am I missing?

+4
source share
1 answer

In the documentation , executereturns the number of rows affected

Returns: Number of rows affected

To get the desired results, you need to go through cursor

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

or use fetchall

cursor.execute('USE CustDB')
tables = cursor.fetchall()
+3

Source: https://habr.com/ru/post/1678724/


All Articles