chore(g3fo-db-ops): sync SKILL.md and db_ops implementation from project

Made-with: Cursor
This commit is contained in:
2026-03-16 16:05:34 +08:00
parent 23df8ffd75
commit cf6dc208ef
2 changed files with 29 additions and 5 deletions
+13 -4
View File
@@ -13,11 +13,20 @@ Guide for performing database operations in the G3FO project, including automate
**本技能不再使用 MCP MySQL**,改用 Python 程序 `scripts/db_ops.py` 执行所有数据库操作。 **本技能不再使用 MCP MySQL**,改用 Python 程序 `scripts/db_ops.py` 执行所有数据库操作。
### 安装依赖 ### 安装依赖与运行前提
```bash > **重要:本技能依赖本机可用的 Python 解释器。若未安装 Python 或 `python` 不在 PATH 中,在 Cursor 的 Shell 里直接执行脚本会出现 Windows `Exit code: 9009`(命令未找到)错误。**
pip install -r .claude/skills/g3fo-db-ops/scripts/requirements.txt
``` 1. 在本机安装 Python(3.8+),并确认命令行中可以直接运行:
```bash
python --version
```
如命令无效,请将 Python 安装目录加入系统 PATH,或使用 `py` / `python3` 等本机实际命令名。
2. 安装依赖:
```bash
pip install -r .claude/skills/g3fo-db-ops/scripts/requirements.txt
```
### 默认连接配置 ### 默认连接配置
+16 -1
View File
@@ -19,6 +19,8 @@ import json
import os import os
import sys import sys
from typing import Any, Optional from typing import Any, Optional
from datetime import date, datetime, time
from decimal import Decimal
try: try:
import mysql.connector import mysql.connector
@@ -88,6 +90,16 @@ def get_connection(config: dict):
) )
def _normalize_value(value: Any) -> Any:
"""将 MySQL 返回的值转换为可 JSON 序列化的类型。"""
if isinstance(value, (datetime, date, time)):
return value.isoformat()
if isinstance(value, Decimal):
# 大多数场景下用 float 即可,避免 JSON 不支持 Decimal
return float(value)
return value
def cmd_query(args: argparse.Namespace, config: dict) -> dict: def cmd_query(args: argparse.Namespace, config: dict) -> dict:
"""执行 SELECT 查询。""" """执行 SELECT 查询。"""
conn = get_connection(config) conn = get_connection(config)
@@ -96,7 +108,10 @@ def cmd_query(args: argparse.Namespace, config: dict) -> dict:
cursor.execute(args.sql, args.params or []) cursor.execute(args.sql, args.params or [])
rows = cursor.fetchall() rows = cursor.fetchall()
cursor.close() cursor.close()
return {"success": True, "data": rows, "rowCount": len(rows)} normalized_rows = [
{k: _normalize_value(v) for k, v in row.items()} for row in rows
]
return {"success": True, "data": normalized_rows, "rowCount": len(normalized_rows)}
except MySQLError as e: except MySQLError as e:
return {"success": False, "error": str(e)} return {"success": False, "error": str(e)}
finally: finally: