python : jdbc 连接 h2数据库,pandas 绘基金净值图

python 2.7 使用jdbc连接数据库,要用到 JayDeBeApi 包,JayDeBeApi 包依赖 JPype1 包.

pip install JayDeBeApi

jdbc 连接 h2数据库,绘基金净值图,h2_jdbc_mpl.py

# -*- coding: cp936 -*-
import os, sys
import jaydebeapi

if len(sys.argv)==2:
    fund1 = sys.argv[1]
else:
    print ' usage: h2_jdbc_mpl.py fundcode'
    sys.exit(4)

if not ( fund1.isdigit() and len(fund1)==6):
    print ' fundcode is 6 位数字.'
    sys.exit(4)

url ='jdbc:h2:tcp://localhost/~/test'
user ='sa'
password =''
dirver ='org.h2.Driver'
jar ='/H2/bin/h2-1.3.jar'
##jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
conn =jaydebeapi.connect(dirver,url,[user,password],jar)
curs = conn.cursor()

select ="select MC from JJMC where DM=" + fund1
curs.execute(select)
fundname = curs.fetchall()
#print(fundname)

import pandas as pd
import pandas.io.sql as psql

query ="select RQ,JZ from JJJZ where DM=%s order by 1" % (fund1)
# dataFrame
df = psql.read_sql_query(query,conn)

curs.close()
conn.close()

import matplotlib.pyplot as plt
# 加这个两句 可以显示中文
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False

fname = fund1 + fundname[0][0] # 基金代码 + 基金名称 
# 基金净值绘图
df['JZ'].plot(figsize=(12,6), grid=True, title=fname)
# 画30日移动平均线  
pd.rolling_mean(df['JZ'], 30).plot(grid=True)
plt.show()

猜你喜欢

转载自blog.csdn.net/belldeep/article/details/81265358