Python27 with Oracle Sample(Windows OS)

Install oracle client

Install python oracle driver

Install the package

Double click the package and click Run to continue to finish installation.

Test if installed successfully

Open windows command line and type below command, if there is no error then it is success.

py
>>> import cx_Oracle

Code with example

import cx_Oracle

def get_connection(url):
    try:
        connection = cx_Oracle.connect(url)
        return connection
    except cx_Oracle.Error as err:
        print err
    return None

def query_demo1(connection):
    cursor = connection.cursor()
    cursor.execute("select * from test_table")
    for result in cursor:
        print result
    cursor.close()
    connection.close()
    return

def query_demo2(connection):
    cursor = connection.cursor()
    cursor.execute("select * from test_table")
    row = cursor.fetchone()
    print row
    row = cursor.fetchone()
    print row
    cursor.close()
    connection.close()
    return

def query_demo3(connection, sql):
    cursor = connection.cursor()
    cursor.execute(sql)
    res = cursor.fetchmany(numRows=3)
    for result in res:
        print result
    cursor.close()
    connection.close()
    return

def query_by_args(connection, sql, args):
    cursor = connection.cursor()
    cursor.prepare(sql)
    cursor.execute(None, args)
    res = cursor.fetchall()
    print res
    cursor.close()
    connection.close()
    return

def query_all(connection, sql):
    cursor = connection.cursor()
    cursor.execute(sql)
    res = cursor.fetchall()
    for result in res:
        print result
    cursor.close()
    connection.close()
    return

def call_func(connection, funcName, returnType, inputParam):
    cursor = connection.cursor()
    #cursor.callfunc('myFunc', cx_Oracle.NUMBER, ('TEST',2))
    res = cursor.callfunc(funcName, returnType, inputParam)
    print res
    cursor.close()
    connection.close()
    return

def call_procedure(connection, procName, in_outParam):
    cursor = connection.cursor()
    #myVar = cursor.var(cx_Oracle.NUMBER)
    #in_outParam = (123, myVar)
    cursor.callproc(procName, in_outParam)
    #print myVar.getValue()
    cursor.close()
    connection.close()
    return

connectionUrl = "<username>/<password>@<url>:<port>/<sid>"
conn = get_connection(url=connectionUrl)
if conn != None :   
    query_demo3(connection=conn,sql="select * from test_table")
    conn = get_connection(url=connectionUrl)
    query_by_args(connection=conn,sql="select * from test_table where id = :id", args={'id':100010027300474})
else: 
    print "Connection is none"

References

http://stackoverflow.com/questions/12538238/python-module-cx-oracle-module-could-not-be-found
http://www.oracle.com/technetwork/articles/dsl/python-091105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值