From 5d97ffac98bc58ee871a081cad1cee9371494770 Mon Sep 17 00:00:00 2001 From: Relakkes Date: Thu, 18 Apr 2024 22:34:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96mysql=E8=A1=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + db.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f1d1357..aedb244 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ ### 数据保存 - 支持保存到关系型数据库(Mysql、PgSQL等) + - 执行 `python db.py` 初始化数据库数据库表结构(只在首次执行) - 支持保存到csv中(data/目录下) - 支持保存到json中(data/目录下) diff --git a/db.py b/db.py index b9d8382..335d8ae 100644 --- a/db.py +++ b/db.py @@ -6,6 +6,7 @@ import asyncio from typing import Dict from urllib.parse import urlparse +import aiofiles import aiomysql import config @@ -62,6 +63,7 @@ async def init_db(): await init_mediacrawler_db() utils.logger.info("[init_db] end init mediacrawler db connect object") + async def close(): """ 关闭连接池 @@ -74,5 +76,21 @@ async def close(): db_pool.close() +async def init_table_schema(): + """ + 用来初始化数据库表结构,请在第一次需要创建表结构的时候使用,多次执行该函数会将已有的表以及数据全部删除 + Returns: + + """ + utils.logger.info("[init_table_schema] begin init mysql table schema ...") + await init_mediacrawler_db() + async_db_obj: AsyncMysqlDB = media_crawler_db_var.get() + async with aiofiles.open("schema/tables.sql", mode="r") as f: + schema_sql = await f.read() + await async_db_obj.execute(schema_sql) + utils.logger.info("[init_table_schema] mediacrawler table schema init successful") + await close() + + if __name__ == '__main__': - asyncio.run(init_db()) + asyncio.get_event_loop().run_until_complete(init_table_schema())