Install
openclaw skills install text2sqlSupport generating SQL queries through natural language; use when users need to configure Text-to-SQL database, manage data topics, or generate SQL with natural language questions
openclaw skills install text2sqlRequired packages and versions for scripts:
pyyaml>=6.0
sqlalchemy>=2.0.0
This Skill generates SQL through the HTTP API /api/sql_for_skill/ endpoint:
https://asksql.ucap.com.cn/--api-url parameter)API Interface Specification:
POST /api/sql_for_skill/multipart/form-dataquestion: User's natural language question (string)yaml_file: YAML configuration file (uploaded as file)[
{
"STATUS": "ok",
"MESSAGE": "",
"SQL": "SELECT SUM(total_amount) AS total_sales FROM orders WHERE YEAR(signing_date) = 2026",
"SQL_NO_PERM": "SELECT SUM(total_amount) AS total_sales FROM orders WHERE YEAR(signing_date) = 2026",
"QUESTION": "This year's total sales"
}
]
SQL field from the first element of the response arrayWhen the agent guides users through data configuration, it must clearly explain the following two methods:
Method 1: Database URL Configuration (Highly Recommended)
Method 2: Excel File Configuration
Excel File Format (Must Strictly Follow):
┌─────────────────────────────────────────────────────────────────┐
│ Excel File: products.xlsx │
├─────────────────────────────────────────────────────────────────┤
│ Sheet: orders (Sheet name = Table name) │
├──────────┬──────────┬────────────┬───────────┬─────────────────┤
│ order_id │ customer │ order_date │ amount │ status │
│ (Column) │ (Column) │ (Column) │ (Column) │ (Column) │
├──────────┼──────────┼────────────┼───────────┼─────────────────┤
│ 1001 │ John │ 2024-01-15 │ 1500.00 │ completed │
│ 1002 │ Mary │ 2024-01-16 │ 2300.50 │ pending │
│ ... │ ... │ ... │ ... │ ... │
└──────────┴──────────┴────────────┴───────────┴─────────────────┘
↑ First row = Column names (field names)
↑ Data starts from second row
Format Requirements:
products.xlsx for products database)orders sheet for orders table)The agent should clearly recommend users to prioritize the database URL configuration method. Only use the Excel file method when users cannot provide a database connection.
python scripts/config_db.py --db-url <database URL> --db-password <password> --config-file ./output/text-to-sql-config.json
This script saves the configuration to ./output/text-to-sql-config.json file.
database_type://username:password@host:port/database_name
Examples:
mysql://root:password@localhost:3306/mydb (will auto-convert to use pymysql driver)mysql+pymysql://root:password@localhost:3306/mydbmssql://sa:password@localhost:1433/mydbNote: For MySQL connections, the system automatically converts mysql:// to mysql+pymysql:// for better compatibility. You can also explicitly specify the driver.
After configuration, use the following command to read table structure:
python scripts/read_tables.py --config-file ./output/text-to-sql-config.json
When users cannot provide a database connection, the Excel file method can be used for configuration.
read_tables.py script:python scripts/read_tables.py --excel-file <Excel file path>
Example:
python scripts/read_tables.py --excel-file data_file.xlsx
--excel-file: Complete path to the Excel fileImportant: The agent must check the configuration status before performing any operation
The agent needs to check whether yaml configuration files exist in the ./output/ directory:
Check Method:
# Check if .yaml files exist in ./output/ directory
ls ./output/*.yaml 2>/dev/null
Check Result Handling:
Scenario A: yaml configuration files exist
Scenario B: No yaml configuration files
The agent must clearly explain the two configuration methods:
Method 1: Database URL Configuration (Highly Recommended)
Method 2: Excel File Configuration
The agent should clearly recommend users to prioritize the database URL configuration method.
If user chooses database URL configuration: Guide user to provide database URL and password, save to local configuration file:
python scripts/config_db.py --db-url <database URL> --db-password <password>
This script saves the configuration to text-to-sql-config.json file.
If user chooses Excel file configuration:
python scripts/read_tables.py --excel-file <Excel file path>
Important: The agent must mandatorily guide users to set up topics, table selection step cannot be skipped
The agent should guide users through the following process:
2.1 Clearly inform user that topic setup is mandatory
2.2 Execute topic setup process
# Method 1: Use database connection (recommended)
python scripts/read_tables.py --config-file ./output/text-to-sql-config.json --output-dir ./output
# Method 2: Use Excel file
python scripts/read_tables.py --excel-file data_file.xlsx
This script generates table_info.json (table name list) and column_info.json (table structure information) files.table_info.json file, displays all tables to user completely, provides clear table descriptionspython scripts/generate_yaml.py --topic-name <topic name> --tables <table list comma-separated> --output-path ./output/
./output/column_info.json directly)2.3 Agent Guidance Recommendations
Important: Step 3 can be executed directly (when Step 0 detects existing configuration)
After configuration is complete (or when existing configuration is detected), users can ask questions in natural language, the agent needs to select topic and generate SQL according to the following logic:
Scenario: One or more topics exist (user has completed topic setup)
Case A: User question explicitly mentions topic
<topic name>.yaml file in ./output/ directoryCase B: User question does not explicitly mention topic
After determining topic, call API to generate SQL:
python scripts/query_sql.py --api-url https://asksql.ucap.com.cn/ --question "user question" --config ./output/<topic name>.yaml
The script calls the API's /api/sql_for_skill/ endpoint and returns the generated SQL statement.
generate_yaml.py to overwrite original configuration fileconfig_db.py to overwrite configuration file./output/ directory--api-url parameter to specify custom API addressAll generated files are stored in ./output/ directory:
table_info.json - Table name listcolumn_info.json - Table structure information<topic_name>.yaml - Topic configuration files./output/text-to-sql-config.jsonquery_sql.py calls API, other scripts are all executed locally