Universal MySQL database query tool supporting all SQL operations.
Install MySQL connector:
pip install mysql-connector-python
Execute SQL directly:
python scripts/mysql_query.py --sql "SELECT * FROM users LIMIT 10"
First time setup (saves to ~/.qclaw/mysql_config.json):
python scripts/mysql_query.py \
--host localhost \
--port 3306 \
--user root \
--password your_password \
--database your_db \
--sql "SELECT 1" \
--save-config
After saving, simply provide --sql:
python scripts/mysql_query.py --sql "SELECT * FROM users"
Temporarily use different connection:
python scripts/mysql_query.py \
--host prod-db.example.com \
--user admin \
--password secret \
--database production \
--sql "SELECT COUNT(*) FROM orders"
Table format (default):
python scripts/mysql_query.py --sql "SELECT * FROM users" --format table
JSON format:
python scripts/mysql_query.py --sql "SELECT * FROM users" --format json
When user provides natural language, convert to SQL first, then execute.
Examples:
| Natural Language | SQL |
|---|---|
| ----------------- | ----- |
| "查询 users 表前 10 条数据" | SELECT * FROM users LIMIT 10 |
| "统计今天的新用户数" | SELECT COUNT(*) FROM users WHERE DATE(created_at) = CURDATE() |
| "查看 orders 表中金额大于 1000 的订单" | SELECT * FROM orders WHERE amount > 1000 |
| "删除测试数据" | DELETE FROM users WHERE is_test = 1 |
Workflow:
mysql_query.py --sql "" User: "查询 users 表前 10 条数据"
python scripts/mysql_query.py --sql "SELECT * FROM users LIMIT 10"
Output:
id | name | email | created_at
----------------------------------------------
1 | Alice | alice@test.com | 2024-01-01 10:00:00
2 | Bob | bob@test.com | 2024-01-02 11:00:00
...
User: "把 user_id=5 的用户名改成 'Charlie'"
python scripts/mysql_query.py --sql "UPDATE users SET name='Charlie' WHERE id=5"
Output:
✅ Query executed successfully
Affected rows: 1
User: "创建一个 test_table 表,包含 id 和 name 字段"
python scripts/mysql_query.py --sql "CREATE TABLE test_table (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100))"
Output:
✅ Query executed successfully
Affected rows: 0
Common errors and solutions:
pip install mysql-connector-pythonFor detailed examples and syntax reference, see:
For complex queries (joins, subqueries, etc.), refer to references/mysql_examples.md for patterns.
Example - JOIN with aggregation:
SELECT
u.name,
COUNT(o.id) as order_count,
SUM(o.amount) as total_amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'
GROUP BY u.id
HAVING order_count > 5
ORDER BY total_amount DESC
LIMIT 20
SELECT
name,
salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) as rank
FROM employees;
See references/mysql_examples.md for more window function examples.
For batch operations, use transactions (autocommit is enabled by default, modify script if needed):
# Modify script to support --no-autocommit flag if needed
Q: Chinese characters appear as question marks?
A: Ensure database/table charset is utf8mb4, add --charset utf8mb4 if needed (modify script to support this).
Q: Connection timeout?
A: Check firewall settings, ensure MySQL allows remote connections (bind-address = 0.0.0.0).
Q: "Incorrect string value"?
A: Ensure utf8mb4 charset is used everywhere (database, table, connection).
共 1 个版本