docker--在Anaconda jupyter 容器中使用oracle数据源时,Oracle客户端安装配置及使用示例

配置oracle 11.2 客户端

  • 将instantclient-basic-linux.x64-11.2.0.4.0.zip解压至/home/jupyter/oracle/
  • 将instantclient-sqlplus-linux.x64-11.2.0.4.0.zip解压/home/jupyter/oracle/【可选,提供sqlplus命令】
  • 复制【操作系统一般都有安装libaio.so cp /usr/lib64/libaio.so.1 /home/jupyter/oracle/instantclient_11_2/
  • ln -s /home/jupyter/oracle/instantclient_11_2/libclntsh.so.11.1 /home/jupyter/oracle/instantclient_11_2/libclntsh.so
bash 复制代码
docker run -d --rm --name=jupyter-server -p 8888:8888 \
	-e  LD_LIBRARY_PATH=/home/oracle/client:$LD_LIBRARY_PATH 
	-v /home/jupyter/notebook:/root/jupyter:rw 
	-v /home/jupyter/oracle/instantclient_11_2:/home/oracle/client 
	-v /home/jupyter/assets:/opt/conda/lib/python3.9/site-packages/notebook/static/components/pyecharts/assets:ro jupyter:2023.8.23

在jupyter中使用示例

导入依赖m

python 复制代码
import cx_Oracle
import pandas as pd
import os
from sqlalchemy import create_engine
os.environ['NLS_LANG']='SIMPLIFIED CHINESE_CHINA.UTF8'
from sqlalchemy import text

使用cursor方式

python 复制代码
conn = cx_Oracle.connect('user/pw@192.168.18.19:1521/orcl')
cursor=conn.cursor()
cursor.execute("select * from USER ")
cursor.fetchall()[0]

pandas方式

原生连接方式

python 复制代码
df=pd.read_sql("select * from USER where IS_STATUS=:status",conn,params={'status':2})
df

使用sqlalchemy方式

python 复制代码
engine = create_engine("oracle+cx_oracle://user:pw@192.168.18.19:1521/orcl",echo=False)
conn = engine.connect()
df=pd.read_sql_query(text("select * from USER where IS_STATUS=1"),con=conn)
df

碰到的坑

  • 'OptionEngine' object has no attribute 'execute'错误
  • Not an executable object:

oracle instant-client下载与安装sqlplus
Centos7-安装oracle客户端11.2.0.4
Linux系统安装oracle客户端步骤详解
sqlalchemy+pandas:错误 'OptionEngine' object has no attribute 'execute','str' object has no attribute '_execute_on_connection'

instant-client for linux-x86-64
instant-client for win64

相关推荐
IVEN_7 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang8 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮8 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling8 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮12 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽12 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers