ApsaraDB for MySQL

更新时间:
复制 MD 格式

Use the ApsaraDB for MySQL node to insert, delete, update, and query data in an ApsaraDB for MySQL table.

Prerequisites

  • You have purchased an ApsaraDB for MySQL instance. For more information, visit the ApsaraDB for MySQL product page.

  • You have created a database and a table. For more information, see ApsaraDB for MySQL databases.

  • You have added 0.0.0.0/0 to the IP whitelist of your ApsaraDB for MySQL instance. For more information, see Configure an IP whitelist.

    Note

    The IP address range 0.0.0.0/0 allows access from any device. Use this with caution.

  • You have created a project and a business service. For more information, see Projects and Create a business service.

Node configuration

In the business logic editor, add an ApsaraDB for MySQL node from the Nodes panel to your business flow to configure it.

Parameter

Description

node name

The name of the node. The name can contain Chinese characters, letters, digits, and underscores (_). It must be 30 characters or less.

username

The username for your ApsaraDB for MySQL instance account. You can find this on the account management page of the instance in the RDS console.

For more information about ApsaraDB for MySQL, see (Deprecated) Usage flow.

password

The password for the ApsaraDB for MySQL instance account.

endpoint

The public endpoint of the instance. You can find the public endpoint on the database connection page of the instance in the RDS console.

Note

If your IoT Studio project and ApsaraDB for MySQL instance are in the same Alibaba Cloud account, region, and VPC, you can also use the internal endpoint to connect to the database.

database name

The name of the database. You can find it on the database management page of the instance in the RDS console.

port number

The public port number of the instance. You can find the public port number on the database connection page of the instance in the RDS console.

operation type

The database operation to perform. The available options are:

  • insert: Inserts data into the database.

  • query: Queries data from the database.

  • delete: Deletes data from the database.

  • update: Updates data in the database.

parameters

The parameters required for the selected operation type. For details, see Parameters below.

Note
  • The ApsaraDB for MySQL node does not support parameter names that contain Chinese characters.

  • When you insert the status of a device, the statusCode field uses numeric codes. You can convert these numeric codes to text before inserting them into the database. Their meanings are as follows:

    • 0: Inactive

    • 1: Online

    • 3: Offline

    • 8: Disabled

Parameters

You can use variables in the parameters. IoT Studio has the following built-in global variables:

  • payload: The output from the previous node. You can access data for a specific key using payload.key_in_object.

    For example, payload.productKey accesses the value from the previous node's output {productKey: 'value'}.

  • query: The data from the entry node, such as the input parameters of an HTTP request node or the device data from a device trigger node. You can access specific data using query.parameter_name.

    For example, query.deviceName accesses the value of the input parameter deviceName in an API request node.

  • node: The output from a specific node (node.node_ID). You can access data for a specific key using node.node_ID.key_in_output_object.

    For example, node.node_39***0.pageNum accesses the value of pageNum from the output of the node_39***0 node.

The following list describes the parameter structure and provides examples for each operation type.

  • insert

    {
        "table": "",  // The name of the table
        "rows": [{}]  // The data to insert
    }

    Example

    {
        "table": "base_info",
        "rows": [
            {
                "id": "{{payload.id}}",
                "user": "{{payload.user}}",
                "ext": "{{payload.ext}}",
                "msg": "{{payload.msg}}"
            }
        ]
    }
  • query

    {
        "table": "",        // The name of the table
        "condition": {      // The query conditions
            "where": {},    // The WHERE clause to filter results
            "columns": [],  // The columns to retrieve
            "orders": [],   // The sorting order
            "limit": 10,    // The page size
            "offset": 1     // The page number
        }
    }

    Example

    {
        "table": "base_info",
        "condition": {
            "where": {"id": "{{query.id}}"},
            "columns": ["id", "user", "ext"],
            "orders": [["id", "desc"]]       
        }
    }
  • delete

    {
        "table": "",      // The name of the table
        "condition": {    // The deletion criteria
        }
    }

    Example

    {
        "table": "base_info",
        "condition": {
        "id": "{{node.node_78***40.id}}"
        }
    }
  • update

    {
        "table": "",          // The name of the table
        "row": {},            // The key-value pairs to update
        "condition": {        // The update conditions
            "where": {},      // The WHERE clause to identify rows to update
            "columns": []     // The columns to update
        }
    }

    Example

    {
        "table": "base_info",
        "row": {"user": "xxx", "ext": "yyy"},
        "condition": {
            "where": {"id": "{{query.id}}"},
            "columns": ["user", "ext"]       
        }
    }

Node output

The node returns the result of the ApsaraDB for MySQL operation.