delete_data
Delete rows from a MySQL table by specifying a table name and a WHERE condition. Supports parameterized queries for safe, injection-free data removal, with optional database configuration.
Instructions
从表中删除数据
Args:
table_name: 表名
condition: WHERE条件子句
params: 条件参数列表
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含删除结果的字典Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes | ||
| condition | Yes | ||
| params | No | ||
| db_config | No |
Implementation Reference
- mysql-mcp.py:409-410 (registration)Tool registration via @mcp.tool() decorator on the delete_data function
@mcp.tool() async def delete_data(table_name: str, condition: str, params: Optional[List[Any]] = None, db_config: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: - mysql-mcp.py:410-455 (handler)Core handler for delete_data tool - accepts table_name, condition, params, and optional db_config, executes DELETE SQL with parameterized WHERE clause, and returns affected_rows count
async def delete_data(table_name: str, condition: str, params: Optional[List[Any]] = None, db_config: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """从表中删除数据 Args: table_name: 表名 condition: WHERE条件子句 params: 条件参数列表 db_config: 数据库连接配置参数,如果为None则使用默认配置 Returns: 包含删除结果的字典 """ if not table_name or not condition: return {"error": "表名和条件不能为空"} if params is None: params = [] try: conn = get_connection(db_config) cursor = conn.cursor() delete_sql = f"DELETE FROM {table_name} WHERE {condition}" cursor.execute(delete_sql, params) conn.commit() return { "success": True, "affected_rows": cursor.rowcount, "message": f"从表 {table_name} 中删除数据成功" } except Error as e: error_message = f"删除数据失败: {str(e)}" if "doesn't exist" in str(e): error_message += f"\n原因:表 {table_name} 不存在" elif "syntax error" in str(e).lower(): error_message += f"\n原因:WHERE条件 '{condition}' 存在语法错误" elif "foreign key constraint fails" in str(e): error_message += "\n原因:删除操作违反了外键约束" return {"error": error_message} except Exception as e: return {"error": f"删除数据时发生未知错误: {str(e)}"} finally: if 'conn' in locals() and conn.is_connected(): cursor.close() conn.close()