标签: cx-oracle

关于元组的新手Python问题

我是Python的新手,我正在使用该cx_Oracle模块编写一些数据库代码.在cx_Oracle文档中,他们有一个代码示例如下:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    error, = exc.args
    print >> sys.stderr, "Oracle-Error-Code:", error.code
    print >> sys.stderr, "Oracle-Error-Message:", error.message
Run Code Online (Sandbox Code Playgroud)

我的问题与创建"错误"对象的位置有关." , ="是做什么的?我尝试搜索Python文档,搜索引擎在搜索运算符时效果不佳.:-)

我知道exc.args是一个单例元组,但我只是不理解" , ="语法.如果我删除逗号,我会收到错误消息" AttributeError: 'tuple' object has no attribute 'code'".

有人能指出我记录的地方吗?谢谢!

编辑:

无需解压缩元组即可实现:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    print >> sys.stderr, …
Run Code Online (Sandbox Code Playgroud)

python cx-oracle tuples

5
推荐指数
2
解决办法
1605
查看次数

使用cx_Oracle更改模式

好吧,我希望这不是重复,搜索没有产生任何有用的东西.

cx_Oracle过去几天我一直在玩弄,安装和使用它.在我遇到当前问题之前,一切都很顺利:我想改变我的架构.如果我使用sqlplus一个简单的'alter session set current_schema = toto;' 会怎么做,但我不知道如何绕过它cx_Oracle.

我已经下载了最新的源代码:cx_Oracle-5.0.2.tar.gz.

根据文档更改架构是一个简单的设置Connection.current_schema,应该是一个读写属性...麻烦是我的Connection对象没有任何current_schema属性.

>>> c = cx_Oracle.connect(...)
>>> dir(c)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', 'autocommit', 'begin', 'cancel', 'changepassword', 'close', 
'commit', 'cursor', 'dsn', 'encoding', 'inputtypehandler',
'maxBytesPerCharacter', 'nencoding', 'outputtypehandler', 'password', 'prepare', 
'register', 'rollback', 'stmtcachesize', 'tnsentry', 'unregister', 'username', 
'version']
Run Code Online (Sandbox Code Playgroud)

尝试使用设置属性

>>> c.current_schema = 'toto'
Run Code Online (Sandbox Code Playgroud)

导致错误... __setattr__显然已被覆盖以防止它.

那么......有谁知道怎么做?


这是我得到的错误.

>>> c.current_schema = 'toto' …
Run Code Online (Sandbox Code Playgroud)

python oracle cx-oracle

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

使用公共密钥和使用Python的cx_Oracle进行身份验证

我用谷歌搜索了一下,但我没有找到任何实质性的结果.是否可以使用基于密钥的身份验证使用Python连接到Oracle服务器?我的目标是能够自动执行我正在使用Python进行的一些报告,而无需在服务器中的任何位置存储用户名/密码.

python authentication oracle cx-oracle public-key-encryption

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

使用cx_Oracle插入CLOB

我正在尝试使用以下代码插入CLOB。

cursor = connection.cursor()
cursor.setinputsizes(HERP = cx_Oracle.CLOB)

cursor.execute("INSERT INTO myTable (FOO, BAR) VALUES (:FOO, :BAR)", FOO=val1, BAR=val2)
cursor.execute("INSERT INTO myTable2 (HERP) VALUES (:HERP)", HERP=val3) 
#len(HERP) 39097
Run Code Online (Sandbox Code Playgroud)

当我运行脚本WITHOUT时,cursor.setinputsizes(HERP = cx_Oracle.CLOB)它在第二个查询WITH上失败ValueError: string data too large,当我运行脚本时cursor.setinputsizes(HERP = cx_Oracle.CLOB),在第一个查询中使用失败DatabaseError: ORA-01036: illegal variable name/number。我尝试插入的CLOB包含一个代码段(即,它具有很多分号,逗号和括号),"string".decode("ascii")返回u'string',所以unicode并不是问题...对吗?我不知道这些东西是否有问题。数据库中的字段当前是一个CLOB,但是我已经使用NCLOB进行了尝试,并且行为没有改变。

我也尝试将字段作为BLOB使用,然后.encode("hex")在我插入的值上使用,再次出现相同的行为。

我也尝试过,HERP = cursor.var(cx_Oracle.CLOB)而不是cursor.setinputsizes(HERP = cx_Oracle.CLOB),同样的问题。

我一直在讨论cx-oracle-users列表上的讨论,但还没有走运。

如果我使用这一行cursor.execute("INSERT INTO myTable2 (HERP) VALUES (:HERP)", HERP="".join(set(val3)).encode("hex")),它将起作用,因此我认为这与数据内容无关(这与BLOB有关)。

如何cx_Oracle将CLOB插入Oracle数据库?

python oracle cx-oracle

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

用户输入变量在cx_Oracle中?

我正在使用cx_Oracle来访问我们的数据库.我希望用户能够输入电台ID,例如:

stationID =(无论用户在提示时输入什么)

cursor.execute('''select cruise, station, stratum
          from union_fscs_svsta
          where station=stationID
          order by cruise''')
Run Code Online (Sandbox Code Playgroud)

因为语句需要是一个字符串,我如何合并一个用户定义的变量?

python oracle cx-oracle

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

cx_Oracle'ORA-01843:unicode参数不是有效月份'

我有以下内容:(使用ipython)

In [30]: con = cx_Oracle.connect('refill_test02/******@MYDB')

In [31]: cur = con.cursor()

In [32]: cur.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'")

In [33]: cur.execute("select to_date(:0), to_timestamp(:1) from dual", ['2013-03-12', '2013-03-12 08:22:31.332144'])
Out[33]: <__builtin__.OracleCursor on <cx_Oracle.Connection to refill_test02@MYDB>>

In [34]: cur.fetchall()
Out[34]: 
[(datetime.datetime(2013, 3, 12, 0, 0),
  datetime.datetime(2013, 3, 12, 8, 22, 31, 332144))]

In [35]: cur.execute("select to_date(:0), to_timestamp(:1) from dual", [u'2013-03-12', '2013-03-12 08:22:31.332144'])
Out[35]: <__builtin__.OracleCursor on <cx_Oracle.Connection to refill_test02@MYDB>>

In [36]: cur.fetchall()
Out[36]: 
[(datetime.datetime(2013, 3, 12, 0, …
Run Code Online (Sandbox Code Playgroud)

python oracle unicode cx-oracle

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

Django - Oracle后端错误

我在Django中有以下模型:

class Event(models.Model):
    # some fields
    start_date = models.DateField()
    end_date = models.DateField()
Run Code Online (Sandbox Code Playgroud)

我正在使用带有Django 1.5和cx_oracle 5.1.2的Oracle 10g数据库.这里的问题是当我尝试在管理界面中创建一个新对象(从日历中选择日期)时,会引发以下错误:

ORA-01843: not a valid month
Run Code Online (Sandbox Code Playgroud)

syncdb创造了DATE在Oracle领域start_dateend_date.这看起来像是后端错误还是我做错了什么?

我确实有其他模型,DateTimeField()当我持久保存新对象时它们工作正常,问题看起来与DateField自身有关.

更新:我检查了后端实现,并在backends/oracle/base.py第513行到第516行:

cursor.execute(
    "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
    " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
    + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
Run Code Online (Sandbox Code Playgroud)

执行此语句允许insert语句具有DATE字段的文字值.我已经检查由后端生成的查询,它是插入'2013-03-20'start_dateend_date.日期匹配NLS_DATE_FORMAT,所以这在理论上应该有效!

更新:我相信我的案例与cx_oracle有关.

更新:因为我仍然没有一个明确的答案(虽然我几乎可以肯定它是导致这个问题的cx_oracle),但是我把我DateField改成了一个DateTimeField …

oracle django cx-oracle django-orm oracle10g

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

cx_Oracle:错误933。ORA-00933:“ SQL命令未正确结束”:SQL命令错误?

之前曾问过类似的问题,但我仍然无法找到解决方案。我的代码:

    try:
        connection = cx_Oracle.connect(ORACLE_CONNECT)
        logger.info("Connection to Oracle success.")
        print ("Oracle DB version: " + connection.version)
        print ("Oracle client encoding: " + connection.encoding)
        print ("Python version: " + platform.python_version())
    except cx_Oracle.DatabaseError as e:
        error, = e.args
        if error.code == 1017:
            print ("Username/password invalid.")
            logger.debug("Username/password invalid: %s", error.code)
        else:
            logger.debug("Database connection error: %s", e)
            print ("Database connection error: %s".format(e))
        raise

    cursor = connection.cursor()
    smsreport_text_new = tuple(smsreport_text)
    find_command = self.identify_unique_msgid(smsreport_list)
    cursor.execute(find_command)

 def identify_unique_msgid(self, smsreport_list):
    msgid_i_to_be_crosschecked = smsreport_list.get('msgid_i')
    msgid_ii_to_be_crosschecked = smsreport_list.get('msgid_ii')
    find_command = …
Run Code Online (Sandbox Code Playgroud)

python sql oracle cx-oracle python-3.x

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

python cx_oracle cursor.rowcount返回0但cursor.fetchall返回数据

我有这个代码,我正在使用包select sqlpython代码执行语句cx_oracle:

import cx_Oracle

try:
    cur = conn.cursor()
    result = cur.execute('select * from table1')
    print(str(cur.rowcount))
    print(cur.fetchall())

except Exception as e:
    print(e)
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码时,我看到了0进来cur.rowcount但是我看到以下数据被打印出来cur.fetchall():

[('185',), ('1860',), ('1908',)]
Run Code Online (Sandbox Code Playgroud)

cx_Oracle包文档确实提到Cursor.rowcount了一个有效的操作,所以我不确定为什么在我的代码中它返回0即使数据即将到来?

python sql database cx-oracle oracle11g

5
推荐指数
2
解决办法
7489
查看次数

如何修复:cx_Oracle.DatabaseError:DPI-1047:无法找到64位Oracle Client库-Python

我正在使用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 python-3.x ubuntu-18.04

5
推荐指数
3
解决办法
4911
查看次数