• api_automation_mysql


    # 操作sql server数据库:增删改查
    import pymssql
    from comm.config.operateconfig import ConfigOperate

    # server 数据库服务器名称或IP:000.000.000.000
    # user 用户名:sa
    # password 密码:123456
    # database 数据库名称:yw
    class SqlServer():
    def __init__(self, path, section):
    # path;配置文件路径
    # 获取数据库基础信息:host,port,DatabaseName,username,passwd
    # path = r' estdataconfigconfig.ini'
    # section = "sqlserver_ay"
    try:
    config = ConfigOperate(path)
    host = config.read(section, 'host')
    # port = config.read(section, 'port')
    DatabaseName = config.read(section, 'DatabaseName')
    username = config.read(section, 'username')
    passwd = config.read(section, 'passwd')
    # 链接数据库
    self.conn = pymssql.connect(host, username, passwd, DatabaseName,charset='GBK', as_dict=False)
    # 获取游标
    self.cursor = self.conn.cursor()
    except pymssql.Error as e:
    print("检查下数据库配置信息是否正确;检查配置信息文件路径获取是否正确:", e)
    # 批量插入数据
    # 查(变量)
    def select(self, sql, *args, **dict):
    try:
    self.cursor.execute(sql, *args, **dict)
    return self.cursor.fetchall()
    except Exception as e:
    print("查询异常,检查下sql语句或看下库里是否有数据:", e)
    finally:
    self.close()

    # 删
    def delete(self, sql, *args, **dict):
    try:
    self.cursor.execute(sql, *args, **dict)
    self.conn.commit()
    except Exception as e:
    print("删除异常:", e)
    finally:
    self.close()

    # 增
    def insert(self, sql, *args, **dict):
    try:
    self.cursor.execute(sql, *args, **dict)
    self.conn.commit()
    except Exception as e:
    print("数据插入异常", e)
    finally:
    self.close()

    def update(self, sql, *args, **dict):
    try:
    self.cursor.execute(sql, *args, **dict)
    self.conn.commit()
    except Exception as e:
    print("数据更新异常,检查下sql语句或看下库里是否有数据", e)
    finally:
    self.close()

    # 关闭连接
    def close(self):
    self.cursor.close()
    self.conn.close()

    if __name__ == '__main__':
    path = r' estdataconfigconfig.ini'
    section = 'sqlserver'
    # sql = " select * from zy..zyys_sqd a where a.zyh = %s and a.sqdbh= %s"
    # oracle = SqlServer(path, section)
    # datas = oracle.select(sql,('00002148','0320071015175709') )
    sql = "select a.mzh from mz..mzys_cfjbxx_fy a (readpast) "
    oracle = SqlServer(path, section)
    datas = oracle.select(sql)
    print(datas)
  • 相关阅读:
    2019-2020-1 20175316 《信息安全系统设计基础》第5周学习总结
    2019-2020-1 20175316 《信息安全系统设计基础》第4周学习总结
    2019-2020-1 20175316 《信息安全系统设计基础》第3周学习总结
    第06组 Alpha冲刺(4/6)
    第06组 Alpha冲刺(3/6)
    第06组 Alpha冲刺(2/6)
    第06组 Alpha冲刺(1/6)
    第06组 团队Git现场编程实战
    团队项目-需求分析报告
    团队项目-选题报告
  • 原文地址:https://www.cnblogs.com/lutong1989/p/14918842.html
Copyright © 2020-2023  润新知