标签: cx-oracle

Python 中的 cx_Oracle、生成器和线程

当连接对象被不同线程使用时,cx_Oracle 游标的行为是什么?生成器将如何影响这种行为?具体来说...

编辑:原来的示例函数不正确;生成器由子函数返回,yield未直接在循环中使用。这澄清finally了何时执行(在return执行之后),但仍然没有回答如果另一个线程开始使用创建游标的连接对象,是否可以使用游标。它实际上似乎(至少在 python 2.4 中)try...finallyyield导致语法错误。

def Get()
  conn = pool.get()
  try:
    cursor = conn.cursor()
    cursor.execute("select * from table ...")
    return IterRows(cursor)
  finally:
    pool.put(conn)

def IterRows(cursor):
  for r in cursor:
    yield r
Run Code Online (Sandbox Code Playgroud)

Get()是一个被多个线程调用的函数。连接是通过threaded=False参数创建的。

我在想...

  1. cursor如果线程 2 出现并使用相同的连接对象,线程 1 的对象是否仍然可用?如果没有,可能会发生什么?

我看到的行为是 cx_Oracle 中谈论协议错误的异常,然后出现段错误。

python multithreading cx-oracle yield generator

5
推荐指数
1
解决办法
3509
查看次数

python 和 cx_Oracle - 动态 cursor.setinputsizes

我正在使用 cx_Oracle 从一个数据库中选择行,然后将这些行插入到另一个数据库中的表中。第二个表的列与第一个选择匹配。所以我有(简化):

db1_cursor.execute('select col1, col2 from tab1')
rows = db1_cursor.fetchall()
db2_cursor.bindarraysize = len(rows)
db2_cursor.setinputsizes(cx_Oracle.NUMBER, cx_Oracle.BINARY)
db2_cursor.executemany('insert into tab2 values (:1, :2)', rows)
Run Code Online (Sandbox Code Playgroud)

这很好用,但我的问题是如何避免 setinputsizes 中的硬编码(我有更多的列)。我可以从 db1_cursor.description 获取列类型,但我不确定如何将它们输入 setinputsizes。即如何将列表传递给 setinputsizes 而不是参数?希望这是有道理的 - python 和 cx_Oracle 的新手

python cx-oracle

5
推荐指数
1
解决办法
6225
查看次数

如何使用 cx_oracle django 包连接到 oracle 旧数据库?

我连接到旧版 oracle 数据库后端的数据库设置是

DATABASES = { 'bannerdb': { 'ENGINE': 'django.db.backends.oracle', 'NAME': 'host:port/service_name', 'USER': 'username', 'PASSWORD': 'password', },

我正在使用此命令运行 create models.py 文件使用

python manage.py inspectdb --database=bannerdb >models.py

我的问题如下

我做了很多研究,但找不到models.py使用 cx_oracle 包为 oracle 数据库后端创建文件的方法,请帮忙。我是一只新蜜蜂。

python oracle django cx-oracle

5
推荐指数
1
解决办法
2344
查看次数

Python cx_Oracle 中 Oracle Prepared Statement 的 IN 子句

我想在 Python 中使用 cx_Oracle 将 IN 子句与准备好的 Oracle 语句一起使用。

例如查询 - select name from employee where id in ('101', '102', '103')

在 python 方面,我有一个列表[101, 102, 103],我将其转换为这样的字符串('101', '102', '103')并在 python 中使用以下代码 -

import cx_Oracle
ids = [101, 102, 103]
ALL_IDS = "('{0}')".format("','".join(map(str, ids)))
conn = cx_Oracle.connect('username', 'pass', 'schema')
cursor = conn.cursor()
results = cursor.execute('select name from employee where id in :id_list', id_list=ALL_IDS)
names = [x[0] for x in cursor.description]
rows = results.fetchall()
Run Code Online (Sandbox Code Playgroud)

这不起作用。难道我做错了什么?

python oracle cx-oracle prepared-statement

5
推荐指数
1
解决办法
4554
查看次数

sqlalchemy 1.1.10 oracle 连接错误

我正在运行此代码以连接到 Oracle 数据库:

engine = sqlalchemy.create_engine("oracle://user:password@dsn")
engine.execute("select 1 from dual")
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:'twophase' is an invalid keyword argument for this function

我正在使用 cx_oracle 6.0b1 和 SQLAlchemy 1.10。当我使用 cx_oracle 5.2.1 和 SQLAlchemy 1.1.5 时,代码有效

知道是什么原因造成的吗?

python oracle cx-oracle sqlalchemy

5
推荐指数
1
解决办法
2715
查看次数

使用 cx_Oracle 从引用游标中检索列名

在使用 cx_Oracle 调用一个简单的存储过程时,我可以轻松地从过程中取回数据,如下所示:

db = db_class() #Sets up stuff, etc.
conn = db.connect() #Returns a connection to the database

cursor = conn.cursor()
l_results = cursor.var(cx_Oracle.CURSOR)

res = cursor.callproc("PROG.DATA.GET_EVENTS", [1,2,l_results]) 
#params = siteID, userID, ref cursor
Run Code Online (Sandbox Code Playgroud)

res[2] 最终成为某种可枚举的,我可以像这样轻松地迭代:

data = [row for row in res[2]]

我最终得到一个列表/元组列表,它为我提供了值,但我还需要列名。我尝试了以下方法:

cols = cursor.description if cursor.description is not None else [] #Returns None
cols = res[2].description if res[2].description is not None else [] 
#Returns an error. Same if l_results is used instead
Run Code Online (Sandbox Code Playgroud)

如何从 l_results 中获取列名?我已经确定 l_results …

python cx-oracle

5
推荐指数
1
解决办法
3511
查看次数

Python cx_oracle 绑定变量与项目列表

我有一个这样的查询:

SELECT prodId, prod_name , prod_type FROM mytable WHERE prod_type in (:list_prod_names)
Run Code Online (Sandbox Code Playgroud)

我想获取产品的信息,取决于可能的类型是: "day", "week", "weekend", "month". 根据日期,它可能至少是这些选项中的一个,或者所有选项的组合。

此信息(列表类型)由函数返回 prod_names(date_search)

我正在使用 cx_oracle 绑定和如下代码:

def get_prod_by_type(search_date :datetime):

  query_path = r'./queries/prod_by_name.sql'
  raw_query = open(query_path).read().strip().replace('\n', ' ').replace('\t', ' ').replace('  ', ' ')

  print(sql_read_op)
  # Depending on the date the product types may be different
  prod_names(search_date)  #This returns a list with possible names
  qry_params = {"list_prod_names": prod_names} # See attempts bellow
  try:
      db = DB(username='username', password='pss', hostname="localhost")
      df = db.get(raw_query,qry_params)
  except Exception:
      exception_error …
Run Code Online (Sandbox Code Playgroud)

sql cx-oracle sqlalchemy python-3.x

5
推荐指数
1
解决办法
3914
查看次数

docker 镜像上的 python 和 oracle 客户端

我想用oracle客户端和python的cx_oracle创建一个docker镜像。我正在使用多阶段 docker 来构建映像,但由于 cx_oracle 无法找到 oracle 客户端库,我缺少一个 env 变量。

FROM oraclelinux:7-slim
RUN  curl -o /etc/yum.repos.d/public-yum-ol7.repo https://yum.oracle.com/public-yum-ol7.repo && \
     yum-config-manager --enable ol7_oracle_instantclient && \
     yum -y install oracle-instantclient18.3-basic oracle-instantclient18.3-devel oracle-instantclient18.3-sqlplus && \
     rm -rf /var/cache/yum && \
     echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient18.3.conf && \
     ldconfig
ENV PATH=$PATH:/usr/lib/oracle/18.3/client64/bin    

FROM python:slim
COPY ./requirement.txt ./requirement.txt
RUN pip install -r ./requirement.txt
COPY --from=0 /usr/lib/oracle/18.3/client64/lib /root/usr/lib/oracle/18.3/client64/lib
COPY --from=0 /usr/lib/oracle/18.3/client64/bin /root/usr/lib/oracle/18.3/client64/bin
ENV PATH=$PATH:/root/usr/lib/oracle/18.3/client64/bin:/root/usr/lib/oracle/18.3/client64/lib
ENV ORACLE_HOME=/root/usr/lib/oracle/18.3/client64/:$ORACLE_HOME
ENV LD_LIBRARY_PATH=/root/usr/lib/oracle/18.3/client64/:$LD_LIBRARY_PATH
RUN echo $PATH
RUN echo $ORACLE_HOME
RUN chmod 755 /root/usr/lib/oracle/18.3/client64/lib/*
RUN …
Run Code Online (Sandbox Code Playgroud)

cx-oracle oracleclient docker

5
推荐指数
1
解决办法
2847
查看次数

与厚模式相比,python-oracledb 瘦模式是否有任何性能影响?

cx_Oracle 在 2022 年 5 月版本中更名为 python-oracledb。它现在有两种模式:薄和厚。厚模式使用Oracle客户端库连接到Oracle,而瘦模式可以直接连接。cx_Oracle 以前总是需要使用 Oracle 客户端库。

使用精简模式而不是粗模式是否会对性能产生影响?

python oracle cx-oracle python-oracledb

5
推荐指数
1
解决办法
3219
查看次数

cx_Oracle.DatabaseError:ORA-12514:TNS:侦听器当前不知道连接描述符中请求的服务

我正在尝试Oracle client 12.2 in RHEL 7 linux从Python程序中对新安装进行健全性测试,但是由于上述错误而失败,不确定我在那里缺少什么.请帮忙解决这个问题:

cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service 
requested in connect descriptor
Run Code Online (Sandbox Code Playgroud)

tnsnames.ora目录下的/home文件

  FRDLD2D1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(Host = frdld2d1.de.db.com)(Port = 1825))
    )
    (CONNECT_DATA =
      (SID = FRDLD2D1)
      )
   )
Run Code Online (Sandbox Code Playgroud)

我的python程序如下

#!/usr/bin/python
import cx_Oracle
#connection = cx_Oracle.connect('PNTH_LOGGINGB_OWNER/password')
connection = cx_Oracle.connect('PNTH_LOGGINGB_OWNER/password@10.245.63.34:1825/orcl')
cursor = connection.cursor()
querystring = "select * from BDR_JOB_MASTER_LOG where ROWNUM <= 1;"
cursor.execute(querystring)
Run Code Online (Sandbox Code Playgroud)

frdld2d1.de.db.com - IP地址 : 10.245.63.34

感谢是否有人在这里发现故障. …

python linux oracle cx-oracle database-connection

4
推荐指数
2
解决办法
4922
查看次数