Random User Email Fetcher

  • Share this:

Code introduction


This function retrieves a random user's email address from a PostgreSQL database table named 'users'. If there are no users in the table, it returns None.


Technology Stack : psycopg2

Code Type : Database Query Function

Code Difficulty : Intermediate


                
                    
def get_random_user_email(cursor):
    # This function fetches a random user email from a PostgreSQL database using psycopg2.
    query = "SELECT email FROM users ORDER BY RANDOM() LIMIT 1;"
    cursor.execute(query)
    result = cursor.fetchone()
    return result[0] if result else None

# JSON Explanation                
              
Tags: