Random Table Selector from Database

  • Share this:

Code introduction


This function selects a random table from the specified database and returns the table name. If there are no tables in the database, it returns None.


Technology Stack : pymysql

Code Type : Database operation

Code Difficulty : Intermediate


                
                    
def select_random_table(cursor, database):
    cursor.execute("SHOW TABLES FROM {}".format(database))
    tables = cursor.fetchall()
    if not tables:
        return None
    table_name = tables[0][0]
    return table_name                
              
Tags: