时间:2021-07-01 10:21:17 帮助过:3人阅读
pip install cx_Oracle-6.0rc1-cp35-cp35m-win_amd64.whl2、安装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?
4. 下面看一个数据库建表和插入操作[python] view plain copy print?
5. 下面再来看看查询,查询通常有两种方式:一种是使用cursor.fetchall()获取所有查询结果,然后再一行一行的迭代;另一种每次通过cursor.fetchone()获取一条记录,直到获取的结果为空为止。看一下下面的例子:[python] view plain copy print?
Python连接ORACLE操作
标签:\n exec sele char pypi 两种 art href while