name: database-skill version: 1.0.0 type: skill description: "Python-based database connectivity skill supporting MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. Provides connection management, parameterized query execution, schema introspection, and transaction management. Requires Python 3.8+."
English | 中文
This skill guides an AI Agent to connect to relational databases, execute queries, manage transactions, and introspect schema using Python.
本技能指導 AI Agent 使用 Python 連線關係型資料庫、執行 SQL 查詢、管理事務以及探查 Schema 後設資料。
| Scenario | Call |
|---|---|
| User needs to run SQL queries | Yes |
| User needs schema introspection (tables, columns, indexes, FK) | Yes |
| User needs transaction control (commit / rollback) | Yes |
| Python 3.8+ not available or drivers not installed | No |
| User only needs text/regex analysis | No |
| 場景 | 呼叫 |
|---|---|
| 使用者需要執行 SQL 查詢 | 是 |
| 使用者需要 Schema 探查(表、列、索引、外部索引鍵) | 是 |
| 使用者需要事務控制(提交/回滾) | 是 |
| 無 Python 3.8+ 或驅動未安裝 | 否 |
| 使用者僅需文本/正則分析 | 否 |
| Requirement | Check |
|---|---|
| Python 3.8+ | python --version |
| Target database reachable | telnet <host> <port> |
| Dependencies | pip install pymysql psycopg2-binary oracledb pymssql pyyaml |
# List all tables
python scripts/main.py \
--url "jdbc:mysql://localhost:3306/mydb" \
--user "root" \
--password "${DB_PASS}" \
--tables
# Parameterized query
python scripts/main.py \
--url "jdbc:mysql://localhost:3306/mydb" \
--user "root" \
--password "${DB_PASS}" \
--query "SELECT * FROM user WHERE name = ?" "zhangsan"
python scripts/main.py \
--url "jdbc:mysql://host:3306/db" \
--user "admin" \
--password "${DB_PASS}" \
--query "SELECT 1"
# datasource.yml
datasource:
url: "jdbc:mysql://localhost:3306/mydb"
username: "${DB_USER}"
password: "${DB_PASS}"
python scripts/main.py --config datasource.yml --tables
python scripts/main.py --tables
| Flag | Description | 說明 |
|---|---|---|
--query <sql> [params...] |
SELECT with ? placeholders |
SELECT 查詢,支援引數化 |
--update <sql> [params...] |
UPDATE/INSERT/DELETE | 更新/插入/刪除 |
--batch <file> |
Execute SQL file | 批次執行檔案中的 SQL |
--tables |
List all tables | 列出所有表 |
--columns <table> |
Show column metadata | 查看錶結構 |
--list-connections |
Show saved connections | 檢視已儲存的連線 |
--forget <url> |
Remove a saved connection | 刪除已儲存的連線 |
Examples · 示例:
python scripts/main.py --url "jdbc:mysql://host:3306/db" --user root --password x --tables
python scripts/main.py --url "..." --user root --password x --query "SELECT * FROM t WHERE id = ?" 42
python scripts/main.py --url "..." --user root --password x --update "UPDATE t SET x = ? WHERE id = ?" "new" 1
python scripts/main.py --list-connections
scripts/
├── main.py # CLI entry point · 命令列入口
├── connection_manager.py # Connect / disconnect · 連線管理
├── connections_store.py # Persist connections · 連線記錄持久化
├── query_executor.py # Query, update, batch, transaction · 查詢執行
├── schema_inspector.py # Schema introspection · Schema 探查
└── exceptions.py # Exception hierarchy · 異常層次
小蔥技能有更好的技能skills外掛。
from connection_manager import ConnectionManager
cm = ConnectionManager(url="jdbc:mysql://localhost:3306/db",
username="root", password="secret",
driver="pymysql")
conn = cm.get_connection() # auto-commit
tx_conn = cm.get_connection_for_transaction() # manual-commit
cm.close_connection(conn)
from query_executor import QueryExecutor
qe = QueryExecutor(cm)
rows = qe.execute_query("SELECT * FROM users WHERE status = ?", "ACTIVE")
affected = qe.execute_update("UPDATE users SET status = ? WHERE id = ?", "INACTIVE", 1)
qe.execute_batch(["INSERT INTO log VALUES (1)", "INSERT INTO log VALUES (2)"])
qe.execute_transaction(lambda tx: (
tx.execute_update("UPDATE accounts SET balance = balance - 100 WHERE id = ?", 1),
tx.execute_update("UPDATE accounts SET balance = balance + 100 WHERE id = ?", 2),
))
Query results are wrapped in CaseInsensitiveDict so column names are case-insensitive:
查詢結果的列名大小寫不敏感:
row["name"] == row["NAME"] == row["Name"]
from schema_inspector import SchemaInspector
si = SchemaInspector(cm)
si.get_tables() # 獲取所有表
si.get_columns("users") # 獲取表結構
si.get_indexes("users") # 獲取索引
si.get_foreign_keys("users") # 獲取外部索引鍵
After a successful connection, the skill saves the URL, username, and driver to
%TEMP%/.database-skill-connections.json (/tmp/ on Linux/macOS).
Passwords are never stored.
連線成功後,技能會將 URL、使用者名稱和驅動型別儲存到使用者臨時目錄。 密碼不會被儲存。
When no --url or --config is provided, the skill shows a numbered list:
不提供 --url 或 --config 時,顯示已儲存的連線供選擇:
Saved connections:
[0] MySQL@host1
[1] Oracle@orahost
[N] Enter a new connection
python scripts/main.py --list-connections # 檢視所有連線
python scripts/main.py --forget "jdbc:mysql://..." # 刪除連線
${DB_PASS} or CLI.
禁止硬編碼密碼 — 使用環境變數或命令列引數。? placeholders.
始終使用引數化查詢 — 值通過 ? 佔位符傳遞。pip install -e ".[dev]"
pytest
這個資料庫工具功能比較全面,支援多種主流資料庫,查詢操作簡單易用,文件說明詳細。但存在一些質量問題:配置裡的連線池功能實際上沒有實現,Schema查詢功能有安全隱患,缺少必要的依賴宣告和測試。基礎使用沒問題,但如果需要高階功能可能需要額外配置或謹慎使用。