You can download this code by clicking the button below.
This code is now available for download.
Selects a random user record from a specified MySQL database.
Technology Stack : mysql-connector-python
Code Type : Database query
Code Difficulty : Intermediate
def select_random_user(conn, db_name):
cursor = conn.cursor()
cursor.execute(f"USE {db_name}")
cursor.execute("SELECT * FROM users ORDER BY RAND() LIMIT 1")
user = cursor.fetchone()
cursor.close()
return user