Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL 'SHOW TABLES' Returns count instead of list (Python)

Tags:

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??

like image 621
Joe Avatar asked Jun 07 '17 16:06

Joe


People also ask

How can I get table record count in MySQL?

To get the count of all the records in MySQL tables, we can use TABLE_ROWS with aggregate function SUM. The syntax is as follows. mysql> SELECT SUM(TABLE_ROWS) ->FROM INFORMATION_SCHEMA.

How can I see tables inside a database?

MySQL SHOW TABLES command example To use the SHOW TABLES command, you need to log on to the MySQL server first. On opening the MySQL Command Line Client, enter your password. Select the specific database. Run the SHOW TABLES command to see all the tables in the database that has been selected.

How do I display the contents of a table in MySQL?

The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.


1 Answers

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()
like image 129
Wondercricket Avatar answered Oct 11 '22 13:10

Wondercricket