oracle数据库操作(结合读取.ini文件操作)
# -*- coding:utf-8 -*-
import cx_Oracle
from Util import readConfig
from Util.log import Logger
Config = readConfig.ReadConfig()
class ConfigOracle:
def __init__(self):
self.dbname = None
self.log = Logger()
self.logger = self.log.get_log()
self.db = None
self.cursor = None
def connectDB(self):
#读取.ini文件里的方法
host = Config.get_db(self.dbname, "host")
username = Config.get_db(self.dbname, "username")
password = Config.get_db(self.dbname, "password")
port = Config.get_db(self.dbname, "port")
database = Config.get_db(self.dbname, "database")
#oracle数据库操作
dns = cx_Oracle.makedsn(str(host), int(port), database)
try:
self.db = cx_Oracle.connect(username, password, dns)
self.cursor = self.db.cursor()
# print("数据库连接成功")
except cx_Oracle.DatabaseError:
self.logger.error("账号密码错误,数据库登录失败")
except ConnectionError as ex:
self.logger.error(str(ex))
def executeSQL(self, sql):
self.connectDB()
try:
self.cursor.execute(sql)
self.db.commit()
except Exception:
self.logger.error("sql为空!")
return self.cursor
# fetchall()函数,它的返回值是多个元组,即返回多个行记录,如果没有结果,返回的是()
def get_all(self, cursor):
value = cursor.fetchall()
return value
# fetchone()函数它的返回值是单个的元组,也就是一行记录,如果没有结果,那就会返回null
def get_one(self, cursor):
value = cursor.fetchone()
return value
def closeDB(self):
self.db.close()
# print("数据库关闭!")
if __name__ == '__main__':
db = ConfigOracle()
db.dbname = "CREDIT5DB"
cursor = db.executeSQL("select t.*,rowid from lb_t_customter_info t where cust_code=(select fk_cust_code from lb_t_into_info where into_app_id='130154740318')")
res = db.get_one(cursor)[3]
print(res)
一切技术都是为业务服务,脱离业务的技术一文不值!

浙公网安备 33010602011771号