一、准备工作

1、安装cx_Oracle
ttps://pypi.python.org/pypi下查找cx_Oracle并下载
执行安装命令 
pip install cx_Oracle-6.0rc1-cp35-cp35m-win_amd64.whl

2、安装ORACLE,并建用户TEST/TEST_lu
二、编写PY文件
import cx_Oracle

conn = cx_Oracle.connect('test/test_lu@orcl1')
print (conn.version) sql = 'select * from lu_test'
cr=conn.cursor()
cr.execute(sql) #执行sql语句
if cr.
print("\nThis is Fetchall!") rs = cr.fetchall() #一次返回所有结果集 print("print all:(%s)" % rs) print("\n print by row:") for x1 in rs:
print(x1) print("\nThis is Fetone!") cr.execute(sql) while (1): rs = cr.fetchone() #一次返回一行 if rs == None: break print(rs) #使用参数查询 print("\n select with parameter:") pr = {'id': 1, 'tel': 'test1'} cr.execute('select * from lu_test where id=:id or name=:tel', pr) #这里我们将参数作为一个字典来处理的 rs = cr.fetchall() print(rs) cr.execute('select * from lu_test where id=:myid or name=:myphone', myid=2, myphone='test2') #这里我们直接写参数 rs = cr.fetchall() print(rs) up=conn.cursor()
up.execute("update lu_test set id=4 where name ='test1'")
conn.commit();
cursor = conn.cursor()
cursor.execute("select * from lu_test")
row = cursor.fetchone()
#print (row[1][0])
#print (row[2][0])
#print (row[0][0])
print(row[1])
row1=cursor.fetchall()
for row_x in row1:
print(row_x) cursor.close()
conn.close()

三、另外的例子
. 创建一个简单的python文件,测试安装是否成功
[python] view plain copy

 

print?

  1. import cx_Oracle
  2. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')
  3. cursor = conn.cursor ()
  4. cursor.execute ("select * from dual")
  5. row = cursor.fetchone ()
  6. print row[0]
  7. cursor.close ()
  8. conn.close ()
4. 下面看一个数据库建表和插入操作
[python] view plain copy

 

print?

  1. import cx_Oracle
  2. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')
  3. cursor = conn.cursor ()
  4. cursor.execute ("CREATE TABLE TEST(ID INT, COL1 VARCHAR(32), COL2 VARCHAR(32), COL3 VARCHAR(32))")
  5. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(1, 'a', 'b', 'c')")
  6. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(2, 'aa', 'bb', 'cc')")
  7. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(3, 'aaa', 'bbb', 'ccc')")
  8. conn.commit()
  9. cursor.close ()
  10. conn.close ()
5. 下面再来看看查询,查询通常有两种方式:一种是使用cursor.fetchall()获取所有查询结果,然后再一行一行的迭代;另一种每次通过cursor.fetchone()获取一条记录,直到获取的结果为空为止。看一下下面的例子:
[python] view plain copy

 

print?

  1. import cx_Oracle
  2. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')
  3. cursor = conn.cursor ()
  4. cursor.execute ("SELECT * FROM TEST")
  5. rows = cursor.fetchall()
  6. for row in rows:
  7. print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])
  8. print "Number of rows returned: %d" % cursor.rowcount
  9. cursor.execute ("SELECT * FROM TEST")
  10. while (1):
  11. row = cursor.fetchone()
  12. if row == None:
  13. break
  14. print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])
  15. print "Number of rows returned: %d" % cursor.rowcount
  16. cursor.close ()
  17. conn.close ()

Python连接ORACLE操作的更多相关文章

  1. python 连接 Oracle 乱码问题(cx_Oracle)

    用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对. 编写的python脚本中需要加入如下几句: import os os.environ['NLS_LANG'] ...

  2. Python 连接Oracle数据库

    连接:python操作oracle数据库  python——连接Oracle数据库 python模块:cx_Oracle, DBUtil 大概步骤: 1. 下载模块 cx_Oracle (注意版本) ...

  3. Python连接oracle数据库 例子一

    step1:下载cx_Oracle模块,cmd--pip install cx_Oracle step2: 1 import cx_Oracle #引用模块cx_Oracle 2 conn=cx_Or ...

  4. python连接Oracle的方式以及过程中遇到的问题

    一.库连接步骤 1.下载cx_Oracle模块 下载步骤 工具 pycharm :File--->右键setting--->找到Project Interpreter  -----> ...

  5. 【python】python连接Oracle数据库

    python连接Oracle数据库 查看Oracle版本 select * from v$version 下载对应版本的InstantClient 下载网址 InstantClient 1.解压Ins ...

  6. Python连接Oracle数据查询导出结果

    python连接oracle,需用用到模块cx_oracle,可以直接pip安装,如网络不好,可下载离线后本地安装 cx_oracle项目地址:https://pypi.org/project/cx_ ...

  7. python 连接 oracle 统计指定表格所有字段的缺失值数

      python连接oracle -- qlalchemy import cx_Oracle as co import pandas as pd from sqlalchemy import crea ...

  8. Python 连接 Oracle数据库

    1.环境设置 [root@oracle ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@oracle ~]# python - ...

  9. python 连接oracle -- sqlalchemy及cx_Oracle的使用详解

    python连接oracle -- sqlalchemy import cx_Oracle as orcl import pandas as pd from sqlalchemy import cre ...

随机推荐

  1. [HNOI2011]括号修复 / [JSOI2011]括号序列

    传送门 Solution 一道题花费了两天的时间-- 在大佬@PinkRabbit的帮助下,终于AC了,感动-- 首先,我们考虑一个括号序列被修改成合法序列需要的次数: 我们需要修改的其实是形如... ...

  2. linux 安装nginx(转)

    转自https://www.cnblogs.com/xiaochongc/p/11527492.html 步骤: 1. 安装所需环境 a)安装gcc:安装 nginx 需要先将官网下载的源码进行编译, ...

  3. springboot连接redis进行CRUD

    springboot连接redis进行CRUD: 1.添加以下依赖: <dependency> <groupId>org.springframework.boot</gr ...

  4. 内存分析工具 MAT 的使用【转】

    转自:http://blog.csdn.net/aaa2832/article/details/19419679/ 1 内存泄漏的排查方法 Dalvik Debug Monitor Server (D ...

  5. 【Python代码】随机抽取文件名列表NameList中的Name作为训练集

    #!/usr/bin/env python #coding=utf-8 #随机抽取一部分图片作为测试集 import random NameList=[]#存储所有图片名字 ''' NameListP ...

  6. 2018-2019-2 网络对抗技术 20165311 Exp 9 Web安全基础

    2018-2019-2 网络对抗技术 20165311 Exp 9 Web安全基础 基础问题回答 实践过程记录 WebGoat安装 SQL注入攻击 1.命令注入(Command Injection) ...

  7. (转)MitmProxy+APPnium安装使用

    MitmProxy+APPnium安装使用 2019年08月19日 11:09:48 jiageibuuuyi 阅读数 61更多 分类专栏: python学习笔记   版权声明:本文为博主原创文章,遵 ...

  8. HBase-集群安装

    需要先启动 HDFS 集群和 ZooKeeper 集群. Hadoop 集群安装:https://www.cnblogs.com/jhxxb/p/10629796.html ZooKeeper 集群安 ...

  9. SVN分支创建与合并

    SVN分支 一个branch是某个development line(通常是主线也即trunk)的一个拷贝,branch存在的意义在于,在不干扰trunk的情况下,和trunk并行开发,待开发结束后合并 ...

  10. Tosca new project Repository as MS SQL Server

    # 然后对应的服务器就能自己给创建对应的数据库