refactor: first use sqlalchemy

main
jackluson 2 years ago
parent 88dbaa0249
commit e47da7bbff

@ -7,6 +7,7 @@ login_cookie="xxx"
snapshot_table_name="fund_morning_snapshot_2021_q1" snapshot_table_name="fund_morning_snapshot_2021_q1"
db_host="127.0.0.1" db_host="127.0.0.1"
db_port="3306"
db_name="fund_work" db_name="fund_work"
db_user="xxx" db_user="xxx"
db_password="xxxx" db_password="xxxx"

@ -34,7 +34,7 @@ def main():
10.高分基金\n \ 10.高分基金\n \
输入") 输入")
if input_value == '1' or input_value == '快照': if input_value == '1' or input_value == '快照':
page_index = 1 page_index = 486
get_fund_list(page_index) # 执行申万行业信息入库 get_fund_list(page_index) # 执行申万行业信息入库
elif input_value == '2' or input_value == '新基入库': elif input_value == '2' or input_value == '新基入库':
acquire_fund_base() # 执行行业股票信息入库 acquire_fund_base() # 执行行业股票信息入库

@ -24,6 +24,8 @@ def acquire_fund_base():
idWorker = IdWorker() idWorker = IdWorker()
print('record_total', record_total) print('record_total', record_total)
if record_total == 0:
return
error_funds = [] # 一些异常的基金详情页如果发现记录该基金的code error_funds = [] # 一些异常的基金详情页如果发现记录该基金的code
def crawlData(start, end): def crawlData(start, end):

@ -0,0 +1,22 @@
'''
Desc:
File: /config.py
Project: config
File Created: Sunday, 14th August 2022 5:57:02 pm
Author: luxuemin2108@gmail.com
-----
Copyright (c) 2022 Camel Lu
'''
import os
from dotenv import load_dotenv
load_dotenv()
env_db_host = os.getenv('db_host')
env_db_port = os.getenv('db_port')
env_db_name = os.getenv('db_name')
env_db_user = os.getenv('db_user')
env_db_password = os.getenv('db_password')
env_db_stock_name = os.getenv('db_stock_name')

@ -1,28 +1,16 @@
import os
import pymysql import pymysql
from dotenv import load_dotenv from config.env import env_db_host, env_db_name, env_db_user, env_db_password, env_db_stock_name
def connect(): def connect():
load_dotenv()
env_db_host = os.getenv('db_host')
env_db_name = os.getenv('db_name')
env_db_user = os.getenv('db_user')
env_db_password = os.getenv('db_password')
connect = pymysql.connect( connect = pymysql.connect(
host=env_db_host, user=env_db_user, password=env_db_password, db=env_db_name, charset='utf8') host=env_db_host, user=env_db_user, password=env_db_password, db=env_db_name, charset='utf8')
return connect return connect
def connect_dict(): def connect_dict():
load_dotenv()
env_db_host = os.getenv('db_host')
env_db_name = os.getenv('db_stock_name')
env_db_user = os.getenv('db_user')
env_db_password = os.getenv('db_password')
connect = pymysql.connect( connect = pymysql.connect(
host=env_db_host, user=env_db_user, password=env_db_password, db=env_db_name, host=env_db_host, user=env_db_user, password=env_db_password, db=env_db_stock_name,
charset='utf8') charset='utf8')
connect_dict = { connect_dict = {
'connect': connect, 'connect': connect,

@ -0,0 +1,36 @@
'''
Desc:
File: /engine.py
Project: db
File Created: Sunday, 14th August 2022 5:41:01 pm
Author: luxuemin2108@gmail.com
-----
Copyright (c) 2022 Camel Lu
'''
import os
import sys
sys.path.append(os.getcwd() + '/src')
from sqlalchemy import create_engine, text
from config.env import env_db_host, env_db_name, env_db_user, env_db_password, env_db_port
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://' + env_db_user + ':'+ env_db_password +'@'+ env_db_host + ':' + env_db_port + '/' + env_db_name
_global_engine = None
def get_engine():
global _global_engine
if _global_engine == None:
_global_engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=True, future=True)
return _global_engine
if __name__ == '__main__':
engine1 = get_engine()
with engine1.connect() as conn:
query = {"fund_code": '000696'}
result = conn.execute(
text("SELECT * FROM fund_morning_snapshot WHERE fund_code = :fund_code"),
query
)
for row in result:
print("row", row)
Loading…
Cancel
Save