第一章 考虑mysql数据存储方式
目录
前言
提示:这里可以
量化开始前需要考虑好使用的数据存储方式,本人选择以mysql存储,保留了扩展可以
支持其他的扩展方式,
添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、抽象存储的父类
抽象存储父类后,未来可以通过替换别的存储方式,实现存储的替换
'
import pandas as pd
class Repoistory():
def saveData(self,data:pd.DataFrame,repoistoryParam:dict):
pass
def readData(self, repoistoryParam: dict) -> pd.DataFrame:
pass
二、实现mysql存储
import pymysql as mysql
import pandas as pd
from sqlalchemy import create_engine
from repoistory.base.repoistory import Repoistory
import configEnv.config as conf
class MysqlRepoistory(Repoistory):
def getMysqlConn(self):
return mysql.connect(
host=conf.config.host,
port=conf.config.port,
user=conf.config.user,
password=conf.config.password,
database=conf.config.database,
charset=conf.config.charset
)
def createMysqlEngine(self):
return create_engine(conf.config.mysql_engine)
def handle_cursor(self, func,*args,**kwargs):
connect = self.getMysqlConn()
try:
cursor_obj = connect.cursor()
func(connect,cursor_obj,*args,**kwargs)
cursor_obj.close()
finally:
connect.close()
def saveData(self,data:pd.DataFrame,repoistoryParam:dict):
eng=self.createMysqlEngine()
tableName=repoistoryParam.get('table_name')
data.to_sql( name=tableName,con=eng, if_exists= "append",index= False)
def readData(self,repoistoryParam:dict)->pd.DataFrame:
eng=self.createMysqlEngine()
query_sql=repoistoryParam.get('query_sql')
# index_col = repoistoryParam.get('index_col')
return pd.read_sql(sql=query_sql, con=eng)
if __name__ == '__main__':
mysql=MysqlRepoistory()
repoistoryParam={"query_sql":"select * from trade_daily where stock_code='000002'"}
db=mysql.readData(repoistoryParam)
print(db)
2.使用mysql存储实例,将来换别的存储方式只需新的存储对象,然后替换掉
代码如下(示例):
if __name__ == '__main__':
mysql=MysqlRepoistory()
repoistoryParam={"query_sql":"select * from trade_daily where stock_code='000002'"}
db=mysql.readData(repoistoryParam)
print(db)
总结
以上是对于存储的定义,后续会使用mysql开始爬虫开发、策略开发、策略选股
版权声明:本文为wlj870705原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。