Develop a Python script that connects to a remote MySQL database, creates a new table with columns for name, age, and email address, inserts sample data into the table, and retrieves all rows from the table. The user should provide the connection details (host, port, username, password, and database name) via command line arguments.
PLAINFORMAT
import sys
import mysql.connector
from mysql.connector import Error

def create_table(connection):
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT, email VARCHAR(255))")
    print("Table created successfully")

def insert_data(connection):
    cursor = connection.cursor()
    sql_query = "INSERT INTO users (name, age, email) VALUES (%s, %s, %s)"
    values = [
        ('John Doe', 28, 'john@example.com'),
        ('Jane Smith', 32, 'jane@example.com')
    ]
    cursor.executemany(sql_query, values)
    connection.commit()
    print(f"{cursor.rowcount} rows inserted")

def fetch_data(connection):
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM users")
    rows = cursor.fetchall()

    print("\nData in the table:")
    for row in rows:
        print(row)

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("Usage: python script.py host port username password dbname")
        exit(1)

    host = sys.argv[1]
    port = int(sys.argv[2])
    user = sys.argv[3]
    password = sys.argv[4]
    database = sys.argv[5]

    try:
        connection = mysql.connector.connect(
            host=host,
            port=port,
            user=user,
            password=password,
            database=database
        )

        if connection.is_connected():
            print("Connected to the MySQL server")
            create_table(connection)
            insert_data(connection)
            fetch_data(connection)

    except Error as e:
        print(f"Error: {e}")

    finally:
        if connection.is_connected():
            connection.close()
            print("\nMySQL connection closed")