Um passo a passo um pouco mais completo:
import MySQLdb connection = MySQLdb.connect( host = 'localhost', user = 'myself', passwd = 'mysecret') # create the connection cursor = connection.cursor() # get the cursor cursor.execute("USE mydatabase") # select the database cursor.execute("SHOW TABLES") # execute 'SHOW TABLES' (but data is not returned) A partir daqui há duas formas de obter as linhas:
tables = cursor.fetchall() # return data from last query ou iterar o cursor diretamente, o que faz streaming em vez de armazenar em buffer:
for (table_name,) in cursor: print(table_name) Alternativamente, passe database='mydatabase' para o connect() e pule o USE. No Python 3, o driver mantido é o PyMySQL ou o mysqlclient; o MySQLdb como pacote autônomo está morto há muito tempo.