zoukankan      html  css  js  c++  java
  • 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()




  • 相关阅读:
    shutil文件去重模块
    Nexus构建npm、yum、maven私有仓库
    centos7添加自定义服务到systemctl
    Sonatype nuxus私有仓库介绍
    rancher单节点备份和恢复
    rancher证书过期X509:certificate has expired or is not ye valid
    清理docker日志
    mysql 9 hash索引和B+tree索引的区别
    mysql 8 索引
    mysql 7 慢查询+慢查询工具
  • 原文地址:https://www.cnblogs.com/bruce-he/p/8120377.html
Copyright © 2011-2022 走看看