You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
[FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
Please do not modify this template :) and fill in all the required fields.
Content
This DEMO is used by the Dify code execution node to query (or manipulate) the database via MYSQL and SQL commands and output the results.
Sandbox
Enter the sandbox container via terminal to install dependencies:
docker exec -it sandbox-docker-name /bin/bash
pip install pymysql # You can append "-i https://mirrors.aliyun.com/pypi/simple" to use the Aliyun mirror source for installation in China
Open the bridge bridge to the container network:
Input Variables
json_data Example
{
"database": {
"host": "ip", // Database server address"port": 3306, // Database connection port number"user": "xxx", // Database username"password": "password", // Database user password"database_name": "xxx"// Database name
}
}
sql_command Example
SQL command
Python3 Code
importpymysql# Import library for interacting with MySQL databaseimportjson# Import library for handling JSON datadefmain(json_data, sql_command):
""" Connect to a MySQL database and execute the given SQL command. Parameters: json_data (str): JSON string containing database connection information. sql_command (str): SQL query command to execute. Returns: dict: Dictionary containing query results or error information. """try:
# Parse JSON string into a Python dictionaryconfig=json.loads(json_data)['database']
# Establish a database connection using the configuration informationconnection=pymysql.connect(
host=config['host'], # Database server hostname or IP addressport=config['port'], # Database connection port numberuser=config['user'], # Database usernamepassword=config['password'],# Database passworddatabase=config['database_name'] # Name of the database to connect to
)
try:
withconnection.cursor() ascursor: # Use context manager to get a database cursor# Execute the SQL commandcursor.execute(sql_command)
# Fetch all query resultsresult=cursor.fetchall()
# Convert results to JSON string formatresult_str=json.dumps(result, ensure_ascii=False)
# Return a dictionary with the query resultsreturn {
"result": result_str
}
finally:
# Ensure the database connection is closed to release resourcesconnection.close()
exceptExceptionase:
# Capture and return any exception informationreturn {
"result": f"Error: {str(e)}"
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Self Checks
Content
Sandbox
Enter the sandbox container via terminal to install dependencies:
Open the bridge bridge to the container network:
Input Variables
json_data Example
sql_command Example
Python3 Code
DSL DEMO
Click to Download DSL DEMO
中文 - Dify 代码执行(Mysql查询)DEMO
Beta Was this translation helpful? Give feedback.
All reactions