• 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()




  • 相关阅读:
    Java实现 LeetCode 740 删除与获得点数(递推 || 动态规划?打家劫舍Ⅳ)
    Python oct() 函数
    Python hex() 函数
    Python ord() 函数
    Python unichr() 函数
    Python chr() 函数
    arm,asic,dsp,fpga,mcu,soc各自的特点
    摄像头标定技术
    自主泊车技术分析
    畸变的单目摄像机标定
  • 原文地址:https://www.cnblogs.com/bruce-he/p/8120377.html
Copyright © 2020-2023  润新知