说明
为批量巡检工具准备
仅作为练习使用
import cx_Oracle
class Database:
# 定义基本属性
c_user = 'admin'
__c_pass = 'admin'
c_url = 'localhost:1521/orcl'
# 定义构造方法
def __init__(self,u,p,l):
self.c_user = u
self.__c_pass = p
self.c_url = l
# 赋值:密码
def set_pass(self):
c_pass = self
return
# 取值:密码
def p_s_get(self):
return self.__c_pass
# 检查连接是否可用
def c_stat(self):
try:
conn = cx_Oracle.connect(self.c_user,self.__c_pass,self.c_url)
conn.close()
c = self.c_user+'@'+self.c_url+' 连接正常!'
return c
except cx_Oracle.DatabaseError as err:
c = self.c_user+'@'+self.c_url+' 连接失败! 错误信息为:'+format(err)
return c
# 执行sql
def exe_sql(self, sql):
conn = cx_Oracle.connect(self.c_user, self.__c_pass, self.c_url)
curs = conn.cursor()
curs.execute(sql)
res = curs.fetchall()
curs.close()
conn.close()
return res
# 用户状态查询
def c_usr_stat(self):
sql = """SELECT username,account_status,lock_date,expiry_date from dba_users order by 2,1"""
try:
conn = cx_Oracle.connect(self.c_user, self.__c_pass, self.c_url)
curs = conn.cursor()
curs.execute(sql)
c = curs.fetchall()
curs.close()
conn.close()
return c
except cx_Oracle.DatabaseError as err:
c = '查询错误,错误信息为:'+format(err)
return c