diff --git a/dsLightRag/Config/__pycache__/Config.cpython-310.pyc b/dsLightRag/Config/__pycache__/Config.cpython-310.pyc index 5111a038..c5ecd364 100644 Binary files a/dsLightRag/Config/__pycache__/Config.cpython-310.pyc and b/dsLightRag/Config/__pycache__/Config.cpython-310.pyc differ diff --git a/dsLightRag/Start.py b/dsLightRag/Start.py index ef423589..71e28f1b 100644 --- a/dsLightRag/Start.py +++ b/dsLightRag/Start.py @@ -147,8 +147,8 @@ async def get_tree_data(): try: pg_pool = await init_postgres_pool() async with pg_pool.acquire() as conn: - async with conn.cursor() as cur: - await cur.execute(""" + # 执行查询 + rows = await conn.fetch(""" SELECT id, title, parent_id, @@ -158,8 +158,6 @@ async def get_tree_data(): FROM knowledge_points ORDER BY parent_id, id """) - rows = await cur.fetchall() - # 构建节点映射 nodes = {} for row in rows: @@ -214,28 +212,29 @@ async def update_knowledge(request: fastapi.Request): pg_pool = await init_postgres_pool() async with pg_pool.acquire() as conn: - async with conn.cursor() as cur: - if update_type == 'prerequisite': - await cur.execute( - """ - UPDATE knowledge_points - SET prerequisite = %s - WHERE id = %s - """, - (json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False), - node_id) - ) - else: # related knowledge - await cur.execute( - """ - UPDATE knowledge_points - SET related = %s - WHERE id = %s - """, - (json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False), - node_id) - ) - await conn.commit() + if update_type == 'prerequisite': + await conn.execute(""" + UPDATE knowledge_points + SET prerequisite = $1 + WHERE id = $2 + """, + json.dumps( + [{"id": p["id"], "title": p["title"]} for p in knowledge], + ensure_ascii=False + ), + node_id) + else: # related knowledge + await conn.execute(""" + UPDATE knowledge_points + SET related = $1 + WHERE id = $2 + """, + json.dumps( + [{"id": p["id"], "title": p["title"]} for p in knowledge], + ensure_ascii=False + ), + node_id) + return {"code": 0, "msg": "更新成功"} except Exception as e: diff --git a/dsLightRag/Util/PostgreSQLUtil.py b/dsLightRag/Util/PostgreSQLUtil.py index c6ccf004..a908d064 100644 --- a/dsLightRag/Util/PostgreSQLUtil.py +++ b/dsLightRag/Util/PostgreSQLUtil.py @@ -19,4 +19,49 @@ POSTGRES_CONFIG = { # 初始化 PostgreSQL 连接池 async def init_postgres_pool(): - return await asyncpg.create_pool(**POSTGRES_CONFIG) \ No newline at end of file + return await asyncpg.create_pool(**POSTGRES_CONFIG) + + +# 查询示例 +async def query_example(pool): + async with pool.acquire() as conn: + # 执行查询 + rows = await conn.fetch('SELECT * FROM your_table LIMIT 10') + # 处理结果 + for row in rows: + print(dict(row)) + return rows + + +# 插入示例 +async def insert_example(pool, data): + async with pool.acquire() as conn: + # 执行插入 + stmt = """ + INSERT INTO your_table (column1, column2) + VALUES ($1, $2) + """ + # 使用参数化查询防止SQL注入 + inserted_id = await conn.fetchval(stmt, data['value1'], data['value2']) + print(f"插入成功!") + return inserted_id + + +# 使用示例 +async def main(): + pool = await init_postgres_pool() + + # 查询示例 + await query_example(pool) + + # 插入示例 + #data_to_insert = {'value1': 'test1', 'value2': 'test2'} + #await insert_example(pool, data_to_insert) + + await pool.close() + + +if __name__ == '__main__': + import asyncio + + asyncio.run(main()) \ No newline at end of file diff --git a/dsLightRag/Util/__pycache__/LightRagUtil.cpython-310.pyc b/dsLightRag/Util/__pycache__/LightRagUtil.cpython-310.pyc index b14b6b81..253fd213 100644 Binary files a/dsLightRag/Util/__pycache__/LightRagUtil.cpython-310.pyc and b/dsLightRag/Util/__pycache__/LightRagUtil.cpython-310.pyc differ diff --git a/dsLightRag/Util/__pycache__/PostgreSQLUtil.cpython-310.pyc b/dsLightRag/Util/__pycache__/PostgreSQLUtil.cpython-310.pyc index acaa3d2b..aada6b85 100644 Binary files a/dsLightRag/Util/__pycache__/PostgreSQLUtil.cpython-310.pyc and b/dsLightRag/Util/__pycache__/PostgreSQLUtil.cpython-310.pyc differ