diff --git a/dcs/dcs.log b/dcs/dcs.log index 5286899..4a657fe 100644 --- a/dcs/dcs.log +++ b/dcs/dcs.log @@ -374,3 +374,9 @@ 2022-03-29 11:09:56.282 | INFO | dcs.tests.server:run:36 - [REQUEST] end 2022-03-29 11:09:56.282 | WARNING | dcs.tests.server:run:37 - communication over! 2022-03-29 11:09:56.283 | WARNING | __main__::21 - Overing... +2022-03-29 11:26:08.833 | INFO | __main__::9 - reading config args... +2022-03-29 11:26:08.834 | INFO | __main__::16 - starting the server... +2022-03-29 11:26:47.684 | INFO | __main__::9 - reading config args... +2022-03-29 11:26:47.684 | INFO | __main__::16 - starting the server... +2022-03-29 11:34:44.764 | INFO | __main__::9 - reading config args... +2022-03-29 11:34:44.764 | INFO | __main__::16 - starting the server... diff --git a/dcs/main.py b/dcs/main.py index 810b5b8..059c44f 100644 --- a/dcs/main.py +++ b/dcs/main.py @@ -2,8 +2,9 @@ from dcs.tests.server import Server from configparser import ConfigParser from loguru import logger +from dcs.tests.database import create_user_info - +create_user_info() logger.add('./dcs.log', rotation='10 MB', enqueue=True, backtrace=True, diagnose=True) logger.info('reading config args...') configFile = '../conf/settings.ini' diff --git a/dcs/tests/database.py b/dcs/tests/database.py index 04a3be7..fc3c0cf 100644 --- a/dcs/tests/database.py +++ b/dcs/tests/database.py @@ -18,7 +18,7 @@ def register(u_name, u_pwd): # 获取数据库操作cursor(游标) cur = conn.cursor() # 编写查询的sql语句 - select_sql = f'select user_password from userinfotest where user_name = "{u_name}"' + select_sql = f'select user_password from user_info where user_name = "{u_name}"' # 执行sql语句 cur.execute(select_sql) # 获取执行结果 fetch_one(),判断结果 @@ -28,7 +28,7 @@ def register(u_name, u_pwd): info = '用户名已存在,注册失败' else: # 注册-> 插入数据,手动commit - insert_sql = 'insert into userinfotest (user_name, user_password) values (%s,%s)' + insert_sql = 'insert into user_info (user_name, user_password, create_time, login_state) values (%s,%s,now(),false)' insert_params = [u_name, sha_pwd] cur.execute(insert_sql, insert_params) conn.commit() @@ -48,7 +48,7 @@ def login(u_name, u_pwd): try: conn = mysql_conn() cur = conn.cursor() - select_sql = f'select user_password from userinfotest where user_name = "{u_name}"' + select_sql = f'select user_password from user_info where user_name = "{u_name}"' cur.execute(select_sql) res = cur.fetchone() if res is None: @@ -59,6 +59,9 @@ def login(u_name, u_pwd): m_pwd = res[0] if m_pwd == sha_pwd: info = '用户' + u_name + '登录成功' + update_sql = f'update user_info set login_state = true where username = {u_name}' + cur.execute(update_sql) + conn.commit() else: info = '密码错误,登录失败' # 关闭连接 @@ -73,7 +76,7 @@ def cancel(u_name): try: conn = mysql_conn() cur = conn.cursor() - select_sql = f'delete from userinfotest where username = "{u_name}"' + select_sql = f'delete from user_info where user_name = "{u_name}"' cur.execute(select_sql) cur.close() conn.close() @@ -116,20 +119,10 @@ def drop_table(table_name: str): print(e) -def create_crawl_result_table(table_name: str): +def create_table(create_sql: str): try: conn = mysql_conn() cur = conn.cursor() - create_sql = f'create table if not exists {table_name} (' \ - f'id int primary key not null auto_increment,' \ - f'crawl_id int not null,' \ - f'time timestamp not null,' \ - f'name varchar(100),' \ - f'college varchar(200),' \ - f'major varchar(200),' \ - f'paper varchar(200)' \ - f')' - cur.execute(create_sql) conn.commit() cur.close() @@ -138,6 +131,30 @@ def create_crawl_result_table(table_name: str): print(e) +def create_crawl_result_table(table_name: str): + create_sql = f'create table if not exists {table_name} (' \ + f'id int primary key not null auto_increment,' \ + f'crawl_id int not null,' \ + f'time timestamp not null,' \ + f'name varchar(100),' \ + f'college varchar(200),' \ + f'major varchar(200),' \ + f'paper varchar(200)' \ + f')' + create_table(create_sql) + + +def create_user_info(table_name: str = 'user_info'): + create_sql = f'create table if not exists {table_name} (' \ + f'id int primary key not null auto_increment,' \ + f'create_time timestamp not null default now(),' \ + f'user_name varchar(100),' \ + f'user_password varchar(200),' \ + f'login_state boolean default false' \ + f')' + create_table(create_sql) + + def write_result2database(res: list, table_name: str, last_crawl_id: int): try: conn = mysql_conn()