zoukankan      html  css  js  c++  java
  • python:封装连接数据库方法

    config.py

    # 数据库测试环境
    name = '***'
    password = '******'
    host_port_sid = '10.**.*.**:1521/bidbuat'

    OracleOperation.py

    import cx_Oracle
    import config
    
    
    class OracleOperation(object):
    
        # 执行下面的execute_sql方法时会自动执行该初始化方法进行连接数据库
        def __init__(self):
            # 建立连接
            self.conn = cx_Oracle.connect(config.name, config.password, config.host_port_sid)
            # 创建游标
            self.cursor = self.conn.cursor()
    
        def execute_sql(self, sql):
            """
            执行sql语句,并commit提交
            :param sql:需要执行的sql语句
            :return:
            """
            self.cursor.execute(sql)
            self.conn.commit()
    
        def get_data(self):
            """
            获得查询数据
            :return: 返回查到的数据
            """
            data = self.cursor.fetchall()
            return data
    
        def close_oracle(self):
            # 关闭游标
            self.cursor.close()
            # 关闭数据库连接
            self.conn.close()
  • 相关阅读:
    完数
    自定义的allocator
    成绩的处理
    R语言-线性回归(1)
    R语言-朴素贝叶斯分类器(1)
    R语言控制流
    leetcode Two sum
    数据库环境搭建
    表单验证制作注册页面
    表单验证
  • 原文地址:https://www.cnblogs.com/gcgc/p/11526803.html
Copyright © 2011-2022 走看看