數據處理模塊:shujuchuli.pysql
shujuchuli數據庫
__init__(self, user, password, dsn, database)app
get_url(self)函數
get_sql(self,code)fetch
# -*- coding: utf-8 -*- class shujuchuli: def __init__(self, user, password, dsn, database): self.user = user self.password = password self.dsn = dsn self.database = database import sqlalchemy self.engine = sqlalchemy.create_engine('mssql+pyodbc://'+user+':'+password+'@'+dsn) def get_url(self): import tushare import pandas codelist = pandas.DataFrame((tushare.get_today_all())['code']) codelist.to_sql('codelist', self.engine, if_exists='replace', index=False) for i in range(0, len(codelist)): stockdata = tushare.get_hist_data(codelist['code'][i]) index = list(stockdata['open'].index) stockdata['date'] = pandas.Series(index, index) pandas.DataFrame(stockdata, index) stockdata.to_sql(codelist['code'][i], self.engine, if_exists='replace', index=False) def get_sql(self, code): connection = self.engine.connect() result = connection.execute('select * from ['+self.database+'].[dbo].['+code+']') stockdata = result.fetchall() return stockdata
調用方法:url
Python 使用 pyodbc 方式鏈接數據庫比較簡單,但需先設置系統 DSN ,方法爲(控制面板 - ODBC - 系統 DSN)spa
# -*- coding: utf-8 -*- import sys sys.path.append('C:\WinPython-32bit-3.4.4.2\my_WorkSpace1\shujuchuli.py') import shujuchuli test = shujuchuli.shujuchuli('sa', '123456', 'XiTongDSN', 'ShuJuKu') test.get_url() test.get_sql('002237')
附圖:code