如何将本地获取jqdata的数据保存为sql数据库文件呢?
简单粗暴直接上代码,使用的是python自带的sqlite3数据库,比较轻便!
import jqdatasdk as jq
from sqlalchemy import create_engine
import sqlite3
import pandas as pd
jq.auth('admin','passpords')
ADDR = 'sqlite:///jqstockdata.db'
engine = create_engine(ADDR)
Stocks = list(jq.get_all_securities(['stock']).index)
connect=sqlite3.connect('jqStockdata.db')
def save(stocks):
print("save code:",stocks)
try:
df = jq.get_price(stocks, start_date='start_date', end_date='end_data',frequency='1d',skip_paused=True,fq='pre')
name =stocks
df.to_sql(name, conn2, if_exists='append')
except:
print(code, " save failed")
for i in range(0, len(Stocks)):
save(Stocks[i])
数据保存好之后我们如何获取数据呢?也很简单,请看下面代码
import sqlite3
import pandas as pd
conn=sqlite3.connect('jqstockdata.db')
df=pd.read_sql('select * from "{}"'.format('股票代码'),index_col = 'index',con= conn )
df.index= pd.to_datetime(df.index,format='%Y-%m-%d')
具体效果我用研究模块直接展示一下
save code: 000001.XSHE
save code: 000002.XSHE
save code: 000004.XSHE
save code: 000005.XSHE
save code: 000006.XSHE
save code: 000007.XSHE
save code: 000008.XSHE
save code: 000009.XSHE
save code: 000010.XSHE
save code: 000011.XSHE
save code: 000012.XSHE
save code: 000014.XSHE
save code: 000016.XSHE
save code: 000017.XSHE
save code: 000018.XSHE
open close high low volume money
index
2018-10-29 11.20 10.75 11.24 10.62 159162878.0 1.725932e+09
2018-10-30 10.78 10.90 11.08 10.73 150182786.0 1.641372e+09
2018-10-31 10.95 10.91 10.99 10.75 115268379.0 1.252157e+09
2018-11-01 10.99 10.83 11.05 10.76 154277632.0 1.679443e+09
2018-11-02 11.04 11.09 11.16 10.83 221292287.0 2.439179e+09