Common automated test script - Oracle database connection

Oracle database connection for # 

#
- * - Coding: UTF-. 8 - * - Import cx_Oracle AS Oracle from DBUtils.PooledDB Import PooledDB class OrclPool (Object): "" " . 1) where a number of functions related to package oracle connection pool; 2) and sid SERVICE_NAME, the program has a value which is determined automatically, if there are two values, using sid default; if one would like to use, only need to empty the other is set as, service_name = '. ' 3) disposed on config, only the type of the value is the port int, others are STR: "" " DEF the __init__ (Self, config): self.conn = OrclPool. __get_conn (config) self.cur = self.conn .cursor () @staticmethod DEF __get_conn (conf): "" " Some PoolDB parameters may be used, according to the actual situation to choose their own mincached: the number of air connections open when you start maxcached: connection pool maximum available number of connections maxshared: connection pool maximum number of connections that can be shared maxconnections : the maximum number of connections allowed blocking: reaches the maximum number is blocked maxusage: maximum single connection multiplexed frequency : param conf: dict Oracle connection information "" " Host, Port, SID, SERVICE_NAME = conf.get ( ' Host ' ), conf.get ( ' Port ' ), conf.get ( ' SID ' ), conf.get ( ' SERVICE_NAME ') dsn = None if sid: dsn = Oracle.makedsn(host, port, sid=sid) elif service_name: dsn = Oracle.makedsn(host, port, service_name=conf.get('service_name')) __pool = PooledDB(Oracle, user=conf['user'], password=conf['passwd'], dsn=dsn, mincached=1, maxcached=5) return __pool.connection() def execute_sql(self, sql, args=None): """ 执行sql语句 : param sql: str sql statement : param args: list parameter list sql statement "" " IF args: self.cur.execute (sql, args) the else : self.cur.execute (sql) DEF fetch_all (Self, sql, args = None): "" " get full results : param sql: str sql statement : param args: list sql statement parameters : return: tuple fetch results " "" self.execute_sql (SQL, args) return self.cur.fetchall () DEF commit_sql (Self, SQL): self.cur.execute (SQL) self.conn.commit() def __del__(Self): "" " In the resource instance is recovered, the connection is closed pool " "" self.cur.close () self.conn.close () IF the __name__ == " __main__ " : orcl_cfg = { ' User ' : 'username ' , # database user name ' passwd ' : ' password ' , # database password ' Host ' : '0.0.0.0 ' , # database link address ' Port ' :3000 , # port ' SID ' : '' , ' SERVICE_NAME ' : ' tpi2hx ', database name # } ORCL = OrclPool (orcl_cfg)

 

Guess you like

Origin www.cnblogs.com/chenri/p/11365694.html