You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects a table from the SQLite database and returns all the column names of that table.
Technology Stack : sqlite3
Code Type : Database query
Code Difficulty : Intermediate
import sqlite3
import random
def get_random_column_names(cursor):
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
if not tables:
return []
table_name = random.choice(tables)[0]
cursor.execute(f"PRAGMA table_info({table_name});")
columns = cursor.fetchall()
return [column[1] for column in columns]