Python连接ORACLE操作

十年热恋 提交于 2020-03-20 08:31:32

一、准备工作

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_Oracleconn = 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.   
  3. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')    
  4. cursor = conn.cursor ()  
  5. cursor.execute ("select * from dual")  
  6. row = cursor.fetchone ()  
  7. print row[0]  
  8.   
  9. cursor.close ()  
  10. conn.close ()  
4. 下面看一个数据库建表和插入操作
[python] view plain copy
 
 print?
  1. import cx_Oracle  
  2.   
  3. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')    
  4. cursor = conn.cursor ()  
  5.   
  6. cursor.execute ("CREATE TABLE TEST(ID INT, COL1 VARCHAR(32), COL2 VARCHAR(32), COL3 VARCHAR(32))")  
  7.   
  8. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(1, 'a', 'b', 'c')")  
  9. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(2, 'aa', 'bb', 'cc')")  
  10. cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(3, 'aaa', 'bbb', 'ccc')")  
  11. conn.commit()  
  12.   
  13. cursor.close ()  
  14. conn.close ()  
5. 下面再来看看查询,查询通常有两种方式:一种是使用cursor.fetchall()获取所有查询结果,然后再一行一行的迭代;另一种每次通过cursor.fetchone()获取一条记录,直到获取的结果为空为止。看一下下面的例子:
[python] view plain copy
 
 print?
  1. import cx_Oracle  
  2.   
  3. conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')    
  4. cursor = conn.cursor ()  
  5.   
  6. cursor.execute ("SELECT * FROM TEST")  
  7. rows = cursor.fetchall()  
  8. for row in rows:  
  9.     print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])  
  10.   
  11. print "Number of rows returned: %d" % cursor.rowcount  
  12.   
  13. cursor.execute ("SELECT * FROM TEST")  
  14. while (1):  
  15.     row = cursor.fetchone()  
  16.     if row == None:  
  17.         break  
  18.     print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])  
  19.       
  20. print "Number of rows returned: %d" % cursor.rowcount  
  21.   
  22. cursor.close ()  
  23. conn.close ()  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!