我在将unicode插入Oracle模式时遇到问题,我认为数据库是Oracle 11g实例,但目前还不确定.我在OS X 10.6.8上使用python 2.6.1(这是python的系统版本),并使用从sourceforge.net下载的cx-Oracle驱动程序模块5.1,构建并安装到virtualenv 1.6.1实例网站包可见.我的脚本如下
import cx_Oracle
connection = cx_Oracle.connect(
"<name>/<password>@<host>/<service-name>"
)
cursor = connection.cursor()
result = cursor.execute(u"create table UNICODE_TEST (id NUMBER(6), text NCLOB not NULL)")
raw_text = open("test.txt",'r').read()
if isinstance(raw_text,str):
raw_text = raw_text.decode("utf_8")
statement = u"insert into UNICODE_TEST (id, text) values (1,'%s')" % raw_text
result = cursor.execute(statement)
Run Code Online (Sandbox Code Playgroud)
我创建一个连接,创建游标,执行一个语句来创建一个测试表,其中包含NUMBER和NCLOB类型的id和text字段.我打开一个文件,其中包含我所知道的以UTF-8编码的文本,将字符串解码为unicode.在unicode字符串中创建插入语句并执行该语句,结果是此错误.
Traceback (most recent call last):
File "unicode-test.py", line 19, in <module>
result = cursor.execute(statement)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 170: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
在将语句插入Oracle模式之前,有些东西试图将我的语句编码为ASCII.所以我开始寻找更好地了解cx-Oracle如何处理unicode,并在我从sourceforge.net下载的cx-Oracle源代码的HISTORY.txt中找到它
从5.0.4到5.1的更改 …
我有这个代码:
dsn = cx_Oracle.makedsn(hostname, port, sid)
orcl = cx_Oracle.connect(username + '/' + password + '@' + dsn)
curs = orcl.cursor()
sql = "select TEMPLATE from my_table where id ='6'"
curs.execute(sql)
rows = curs.fetchall()
print rows
template = rows[0][0]
orcl.close()
print template.read()
Run Code Online (Sandbox Code Playgroud)
当我这样做时print rows,我明白了:
[(<cx_Oracle.LOB object at 0x0000000001D49990>,)]
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时print template.read(),我收到此错误:
cx_Oracle.DatabaseError:句柄无效!
我如何获取和读取这些数据?谢谢.
这是我做的:
instantclient-basic-nt-11.2.0.3.0.zip,解压缩,然后把它放进去C:\Program Files\Oracle\instantclient_11_2.Path环境变量中.ORACLE_HOME将此路径作为其值.cx_Oracle-5.1.2-11g.win32-py2.7.msi.在跑步的时候,import cx_Oracle我得到的就是
Traceback (most recent call last):
File "<string>", line 2, in <module>
ImportError: DLL load failed: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.
Run Code Online (Sandbox Code Playgroud)
我显然已经卸载/重新安装了cx_Oracle几次,但似乎没有任何帮助.任何人都可以提供一个线索如何解决这个问题?
UPDATE
我跑了Dependency Walker,它遇到了很多麻烦.但是,第一个丢失的.dll(msvcr80.dll)实际上存在于C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.91_x-ww_0de56c07.

在cx_Oracle(或一般的Oracle)中,是否可以为每个查询分配游标,或者在多个查询中重用游标.
def getSomeData(curs): # case 1: pass in a cursor, which is generally
curs.execute('select ...') # reused across queries
return curs.fetchall()
def getSomeData(conn): # case 2: pass in a connection,allocate
curs=conn.cursor() # a cursor for this query
curs.execute('select ...')
return curs.fetchall()
Run Code Online (Sandbox Code Playgroud)
当然,两种方法都返回相同的数据.
这两种方法之间有什么权衡?特别是效率更高还是更低?在许多查询中重用游标是否有任何潜在的缺陷?
这是我的代码.我想找到一种方法,将查询结果作为字典列表而不是元组列表返回.似乎cx_oracle支持这一点,部分文档谈论'绑定'.虽然我无法弄清楚它是如何工作的.
def connect():
dsn = cx_Oracle.makedsn("host", 1521, "sid")
orcl = cx_Oracle.connect('scott/tiger@' + dsn)
curs = orcl.cursor()
sql = "select * from sometable"
curs.execute(sql)
result = curs.fetchall()
for row in result:
print row[13] #CATEGORY field order
print row['CATEGORY'] # <- I want this to work ('CATEGORY' is the name of a field in the 'sometable' table)
curs.close()
Run Code Online (Sandbox Code Playgroud) 我有两台RHEL服务器分别运行Python 2.4和2.6.我需要访问的其他服务器上有一个Oracle数据库.
我试图在我的RHEL服务器上安装cx_oracle,但发现必须首先安装Oracle客户端.
问题是,我没有权限在两台RHEL服务器上安装Oracle客户端.在相同的服务器上,Perl程序可以使用以下命令连接到Oracle数据库:
DBI->connect("dbi:Oracle:host=myhost.prod.com;sid=prddb",'username','password')
Run Code Online (Sandbox Code Playgroud)
没有安装cx_oracle和Oracle客户端,Python可以做同样的事吗?或者有没有关于如何自己编写模块来做同样事情的建议?
提前致谢!
所以我发现帮助安装cx_Oracle但仍然卡住了.我从oracle下载了最新的instantclient,并将ORACLE_HOME设置为解压缩文件的位置(直接和ORACLE_HOME值与文件之间的bin文件夹),但easy_install在运行setup.py时弹出错误说它可以'找到Oracle包含文件.我注意到文件夹中只有11g dll,我是否需要所有3个驱动程序才能完成设置?如果是这样,我在哪里获得它们?
我有一些月度天气数据,我想插入到Oracle数据库表中,但我想在批处理中插入相应的记录,以提高效率.任何人都可以建议我如何在Python中执行此操作?
例如,假设我的表有四个字段:工作站ID,日期和两个值字段.记录由站ID和日期字段(组合键)唯一标识.我必须为每个工作站插入的值将保存在具有X个完整年份数据的列表中,因此,例如,如果有两年的值,则值列表将包含24个值.
我假设如果我想一次插入一个记录,下面就是我这样做的方式:
connection_string = "scott/tiger@testdb"
connection = cx_Oracle.Connection(connection_string)
cursor = cx_Oracle.Cursor(connection)
station_id = 'STATION_1'
start_year = 2000
temps = [ 1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1, 3 ]
precips = [ 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8 ]
number_of_years = len(temps) / 12
for i in range(number_of_years):
for j in range(12):
# make a date for the first day of the month
date_value = datetime.date(start_year + i, j …Run Code Online (Sandbox Code Playgroud) 我是一个Python新手,我在使用绑定变量时遇到了麻烦.如果我执行下面的代码一切正常.
bind= {"var" : "ciao"}
sql = "select * from sometable where somefield = :bind"
cur.prepare(sql)
cur.execute(sql,bind)
Run Code Online (Sandbox Code Playgroud)
相反,如果我添加另一个绑定变量,我得到一个错误.
bind= {"var" : "ciao"}
sql = "select * from sometable where somefield = :bind and otherfield = :bind"
cur.prepare(sql)
cur.execute(sql,(bind,bind))
cur.execute(sql,(bind,bind))
Oracle.NotSupportedError: Variable_TypeByValue(): unhandled data
Run Code Online (Sandbox Code Playgroud)
我用它解决了
cur.execute(sql,(bind["var"],bind["var"]))
Run Code Online (Sandbox Code Playgroud)
但我无法理解为什么以前的命令不行.
哪个是使用绑定变量的正确方法?我正在使用cx_Oracle.
新手在这里尝试使用python做一些数据库分析.我一直收到错误:"错误:找不到Oracle软件安装"安装CX_oracle时(通过easy_install).
问题是我的本地机器上没有oracle,我正在尝试使用python连接到主oracle服务器.我已经设置了另一个程序来执行此操作(visualdb),我有一个.jar文件,我用作驱动程序,但我不知道如何在这种情况下使用它.
有什么建议?
cx-oracle ×10
python ×10
oracle ×6
database ×2
sql ×2
batch-insert ×1
dll ×1
easy-install ×1
oracle11g ×1
python-2.6 ×1
python-2.7 ×1
unicode ×1
windows ×1