Edits history of script submission #6006 for ' Execute arbitrary Query in Python (mysql)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    from typing import TypedDict
    import mysql.connector as mysql_connector
    
    # Define the MySQL resource type
    class mysql(TypedDict):
        ssl: bool
        host: str
        port: float
        user: str
        database: str
        password: str
    
    def main(mysql_credentials: mysql, query; str) -> str:
        # Connect to the MySQL database using the provided credentials
        connection = mysql_connector.connect(
            host=mysql_credentials["host"],
            user=mysql_credentials["user"],
            password=mysql_credentials["password"],
            database=mysql_credentials["database"],
            port=int(mysql_credentials["port"]),
            ssl_disabled=not mysql_credentials["ssl"],
        )
    
        # Create a cursor object
        cursor = connection.cursor()
    
        # Execute the query
        cursor.execute(query)
    
        # Fetch one result
        result = cursor.fetchone()
    
        # Close the cursor and connection
        cursor.close()
        connection.close()
    
        # Return the result
        return str(result[0])

    Submitted by hugo697 372 days ago

  • python3
    from typing import TypedDict
    import mysql.connector as mysql_connector
    
    # Define the MySQL resource type
    class mysql(TypedDict):
        ssl: bool
        host: str
        port: float
        user: str
        database: str
        password: str
    
    def main(mysql_credentials: mysql, query; str) -> str:
        # Connect to the MySQL database using the provided credentials
        connection = mysql_connector.connect(
            host=mysql_credentials["host"],
            user=mysql_credentials["user"],
            password=mysql_credentials["password"],
            database=mysql_credentials["database"],
            port=int(mysql_credentials["port"]),
            ssl_disabled=not mysql_credentials["ssl"],
        )
    
        # Create a cursor object
        cursor = connection.cursor()
    
        # Execute the query
        cursor.execute(query)
    
        # Fetch one result
        result = cursor.fetchone()
    
        # Close the cursor and connection
        cursor.close()
        connection.close()
    
        # Return the result
        return str(result[0])

    Submitted by henri186 794 days ago