Random Row Fetcher Using MySQL-Connector-Python

  • Share this:

Code introduction


This function uses the mysql-connector-python library to execute a SQL query with a cursor object and randomly returns one row from the query results.


Technology Stack : mysql-connector-python, cursor, execute, fetchone

Code Type : Database query

Code Difficulty : Intermediate


                
                    
def fetch_random_data(cursor, query):
    cursor.execute(query)
    random_row = cursor.fetchone()
    return random_row