Skip to content

MySQL

This example demonstrates how to use the MySQL connector in ydata-sdk.

Don't forget to set up your license key

    import os

    os.environ['YDATA_LICENSE_KEY'] = '{add-your-key}'

Example Code

"""Example file to run the Microsoft SQL Server and Azuere SQL Server
connector."""
from ydata.connectors import MySQLConnector

if __name__ == "__main__":

    USERNAME = "insert-username"
    PASSWORD = "insert-password"
    HOSTNAME = "insert-hostname"
    PORT = "3306"
    DATABASE_NAME = "berka"

    conn_str = {
        "hostname": HOSTNAME,
        "username": USERNAME,
        "password": PASSWORD,
        "port": PORT,
        "database": DATABASE_NAME,
    }

    # Create the cnnection to the database
    conn = MySQLConnector(conn_string=conn_str)

    # test the connection
    conn.connection.connect()

    # Get the details from a selected schema from the database
    # For AzureSQLServer schema is a mandatory input
    print(conn.get_database_schema())

    database = conn.read_database()
    print(database)

    # Read a sample of data
    sample = conn.query_sample("SELECT * FROM trans;")
    print(sample)

    # Read a given table from AzureSQL selecte schema
    table_account = conn.get_table(table="account")
    print(table_account)

    print(table_account.to_pandas())
    print(table_account.to_numpy())
    # Reading a query from a database
    # For AzureSQL server it is mandatory to keep the schema reference for each table
    result_query = conn.query("SELECT * FROM trans;")

    # Write the content of a Dataset into a new table
    # Only creation of new tables is supported
    # If trying to write the information into an already existing table, an exception will be raised
    conn.write_table(data=result_query, name="new_account", if_exists="replace")