一、下载oracle客户端,instantclient
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
版本必须比oracle版本要高,查看oracle版本命令为
sqlplus / as sysdba
select * from v$version
二、配置环境变量
三、代码调用
def __init__(self):
# 当前环境
instance = YmlInstance("api.yaml")
current_env = instance.get_param("current_env")
instance = YmlInstance(current_env)
host = instance.get_param("sql").get("host")
port = 1521
user = instance.get_param("sql").get("user")
passwd = str(instance.get_param("sql").get("passwd"))
db = instance.get_param("sql").get("db")
try:
dsn = oracle.makedsn(host, port, service_name=db)
self.connect = oracle.connect(user, passwd, dsn)
self.cursor = self.connect.cursor()
except Exception as e:
log.logger.info("返回结果%s" % e)
def get_result(self, sql):
self.cursor.execute(sql)
result = self.cursor.fetchall()
# 关闭连接
self.cursor.close()
self.connect.close()
return result