Spaces:
Paused
Paused
Download db_manager.py from phanquocdat123/Ai_Agent: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/phanquocdat123/Ai_Agent/resolve/main/db_manager.py
- Command line
-
hf download hf://spaces/phanquocdat123/Ai_Agent/db_manager.py
-
curl -L -o db_manager.py https://huggingface.co/spaces/phanquocdat123/Ai_Agent/resolve/main/db_manager.py
1.45 kB
| import sqlite3 | |
| import config | |
| from contextlib import closing | |
| from config import DB_NAME | |
| def init_db(): | |
| with closing(sqlite3.connect(config.DB_NAME)) as conn: # Dùng config.DB_NAME | |
| # Đảm bảo đúng 4 cột: id, category, content, created_at | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS knowledge | |
| (id TEXT PRIMARY KEY, category TEXT, content TEXT, created_at REAL) | |
| """) | |
| conn.commit() | |
| def save_note(doc_id, category, content, timestamp): | |
| with closing(sqlite3.connect(DB_NAME)) as conn: | |
| # Giờ thì 4 giá trị (?, ?, ?, ?) sẽ khớp với 4 cột ở trên | |
| conn.execute("INSERT INTO knowledge VALUES (?, ?, ?, ?)", (doc_id, category, content, timestamp)) | |
| conn.commit() | |
| def get_all_categories(): | |
| with closing(sqlite3.connect(config.DB_NAME)) as conn: | |
| cursor = conn.execute("SELECT DISTINCT category FROM knowledge") | |
| return [row[0] for row in cursor.fetchall()] | |
| def get_notes_by_category(category): | |
| with closing(sqlite3.connect(config.DB_NAME)) as conn: | |
| cursor = conn.execute("SELECT content FROM knowledge WHERE category = ?", (category,)) | |
| return [row[0] for row in cursor.fetchall()] | |
| def delete_notes(target): | |
| with closing(sqlite3.connect(config.DB_NAME)) as conn: | |
| conn.execute("DELETE FROM knowledge WHERE category LIKE ? OR content LIKE ?", (f"%{target}%", f"%{target}%")) | |
| conn.commit() |