Python3 -- PySQL -- 将函数封装在类中

 

------------------ comm_functions.py ------------

import pymysql

class common_functions(object):

  # ---- 需要初始化 __init__ 来连接数据库,并产生 self.cur 供后面函数应用
def __init__(self):
self.conn = pymysql.connect(
host='192.168.68.189',
port=3307,
user='xxxxx',
passwd='xxxx',
db='xxxx',
)
self.cur = self.conn.cursor()

def execute_sqlscript_fetchall_contents(self, sqlscript):
self.cur.execute(sqlscript)
contents = self.cur.fetchall()
return contents

def execute_sqlscript_fetchall_content(self, sqlscript):
contents = self.execute_sqlscript_fetchall_contents(sqlscript)
for row in contents:
data = row[0]
return data

def calculate_sharecycle(self, open_time, clean_time):
sql_sharecycle = "select count(*) from hd_trading_date where open_day >= left(" + open_time + ", 8) and open_day <= left(" + clean_time + ",8)"
sharecycle = self.execute_sqlscript_fetchall_content(sql_sharecycle)
if open_time[8:12] >= "2100":
sharecycle = sharecycle - 1
if clean_time[3][8:12] >= "2100":
sharecycle = sharecycle + 1


------- gen_report.py ----------
import sys
sys.path.append("./sqldata")
from com_functions import common_functions


def get_data():
sharecycle = common_functions().calculate_sharecycle("20171018143914", "20171018210220")
print("sharecycle: %d" % sharecycle)

if __name__ == "__main__":
get_data()
common_functions().close_sql()




转载于:https://www.cnblogs.com/bruce-he/p/8120377.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值