commit 240e8e56bf7b72880c10cb7dd0e46f6f4a7f290b Author: roger_home_pc Date: Sun Aug 13 22:00:11 2023 +0800 first commit diff --git a/db.py b/db.py new file mode 100644 index 0000000..9cc96e9 --- /dev/null +++ b/db.py @@ -0,0 +1,35 @@ +import pymysql +from pymysql.cursors import DictCursor +from data_dict import special_symbols, escape_list + +db_config = { + "host": "192.168.66.101", + "user": "root", + "passwd": "Sxzgx1209", + "port": 3306, + 'database': 'scrapy' +} + +SELECT_SQL = "SELECT * FROM scrapyh s WHERE s.id = %s;" + + +class DbAction: + def __init__(self): + self.conn = pymysql.Connect(**db_config) + self.cursor = self.conn.cursor(cursor=DictCursor) + + @staticmethod + def decode_pwd(_pwd): + for k, v in special_symbols.items(): + if k in _pwd: + _pwd = _pwd.replace(k, v) + for item in escape_list: + if item[0] in _pwd: + _pwd = _pwd.replace(item[0], item[1]) + return _pwd + + def get_data_by_id(self, _id): + self.cursor.execute(SELECT_SQL, (_id,)) + result = self.cursor.fetchone() + result['unzip_pwd'] = self.decode_pwd(result['unzip_pwd']) + return result