我的目标是使用 Pyinstaller 从一个使用 Tkinter 和 cx_oracle 访问数据库的简单 python 脚本中捆绑一个 exe 文件。python代码是在安装了Anaconda、cx_oracle包和oracle客户端的windows机器上开发的。然后我需要在许多没有oracle 客户端或 Python 的目标 Windows 机器上运行 exe 文件。
我在开发机器上使用 Python 2.7 和 Pyinstaller 3.1。
我在网上搜索了一段时间,但只找到了一个教程:https : //mail.python.org/pipermail/tutor/2014-December/103608.html
我按照相同的步骤修改了规范文件,如下所示:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['mycode.py'],
pathex=['C:\\Users\\myuser\\PycharmProjects\\mycode'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries + [('oraociei11.dll','D:\ProgramFiles\Anaconda2\oraociei11.dll','BINARY')],
a.zipfiles,
a.datas,
name='mycode',
debug=False,
strip=False,
upx=True,
console=True )
Run Code Online (Sandbox Code Playgroud)
捆绑工作。代码在安装了 oracle 客户端的原始机器上运行。但是在没有 oracle 客户端的单独机器上,它无法运行,并显示以下错误消息:
Tkinter 回调 Traceback …
我正在使用带有UTF-16编码的Oracle 数据库。直接使用 cx_oracle 客户端时,可以正确显示变音符号。连接语句是这样的:
cx_Oracle.connect(username, password, conn_str, encoding='UTF-16', nencoding='UTF-16')
Run Code Online (Sandbox Code Playgroud)
但是,现在我正在构建更大的应用程序,我想SQLalchemy在Flask.
代码如下所示:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
db.Model.metadata.reflect(db.engine)
class MyTable(db.Model):
__table__ = db.Model.metadata.tables['mytable']
for row in MyTable.query:
print(row.column_with_diacritics)
Run Code Online (Sandbox Code Playgroud)
上面代码的输出:aoe
但是数据库中的列值是:áóé
所以我的问题是,如何将参数传递encoding='UTF-16', nencoding='UTF-16'给 sqlalchemy 在底层使用的 cx_oracle?
感谢您提供任何建议或其他解决方法。
当连接对象被不同线程使用时,cx_Oracle 游标的行为是什么?生成器将如何影响这种行为?具体来说...
编辑:原来的示例函数不正确;生成器由子函数返回,yield未直接在循环中使用。这澄清finally了何时执行(在return执行之后),但仍然没有回答如果另一个线程开始使用创建游标的连接对象,是否可以使用游标。它实际上似乎(至少在 python 2.4 中)try...finally会yield导致语法错误。
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参数创建的。
我在想...
cursor如果线程 2 出现并使用相同的连接对象,线程 1 的对象是否仍然可用?如果没有,可能会发生什么?我看到的行为是 cx_Oracle 中谈论协议错误的异常,然后出现段错误。
我正在使用 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 将 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)
这不起作用。难道我做错了什么?
我正在运行此代码以连接到 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 时,代码有效
知道是什么原因造成的吗?
我有一个这样的查询:
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) 我想用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 7和python 3.6.7建立到oracle 11g的远程服务器上的连接。我在Ubuntu 18.04中的操作系统
我已经用libclntsh.so安装了Oracle Instant Client库,但没有得到预期的输出。
这是我用来连接到Oracle数据库的代码
connection = cx_Oracle.connect("username/password@host/port")
print (connection.version)
connection.close()
Run Code Online (Sandbox Code Playgroud)
当脚本运行时,我希望获得连接版本,而不是得到以下错误消息
文件“ script.py”,第13行,连接= cx_Oracle.connect(“ username / password @ host / port”)cx_Oracle.DatabaseError:DPI-1047:无法找到64位Oracle Client库:“ libclntsh.so:无法打开共享对象文件:没有这样的文件或目录。请参阅 https://oracle.github.io/odpi/doc/installation.html#linux获取帮助
cx_Oracle 在 2022 年 5 月版本中更名为 python-oracledb。它现在有两种模式:薄和厚。厚模式使用Oracle客户端库连接到Oracle,而瘦模式可以直接连接。cx_Oracle 以前总是需要使用 Oracle 客户端库。
使用精简模式而不是粗模式是否会对性能产生影响?
cx-oracle ×10
python ×6
oracle ×4
python-3.x ×3
sqlalchemy ×3
docker ×1
encoding ×1
generator ×1
oracleclient ×1
pyinstaller ×1
sql ×1
ubuntu-18.04 ×1
yield ×1