Retrieving Table Column Names with DESCRIBE Statement

  • Share this:

Code introduction


This function accepts a database cursor and a table name as parameters, executes a DESCRIBE statement to retrieve column information for the specified table, and returns a list containing the column information. If there is no column information to return, it returns None.


Technology Stack : PyMySQL

Code Type : Database query

Code Difficulty : Intermediate


                
                    
def fetch_random_column_name(cursor, table_name):
    cursor.execute(f"DESCRIBE {table_name}")
    columns = cursor.fetchall()
    return columns if columns else None                
              
Tags: