You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects a column name from the specified database table.
Technology Stack : PyMySQL, pymysql, random
Code Type : Database query
Code Difficulty : Intermediate
def select_random_column(pymysql_connection, table_name):
import pymysql
import random
cursor = pymysql_connection.cursor()
cursor.execute(f"DESCRIBE {table_name}")
columns = cursor.fetchall()
random_column = random.choice(columns)[0]
cursor.close()
return random_column