MySQL Database Version Retrieval Function

  • Share this:

Code introduction


This function connects to a MySQL database and prints the database version information.


Technology Stack : pymysql, connection, cursor, DictCursor

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_select_connection_info(host, user, password, db):
    import pymysql
    from pymysql.cursors import DictCursor
    
    try:
        connection = pymysql.connect(host=host, user=user, password=password, db=db, cursorclass=DictCursor)
        with connection.cursor() as cursor:
            cursor.execute("SELECT VERSION()")
            version = cursor.fetchone()
            print(f"Database version : {version['VERSION()']}")
    finally:
        connection.close()