python连接oracle查询数据_python连接oracle数据库

首先我们要安装一下包:

cx_Oracle-5.3.tar.gz

instantclient-basic-linux.x64-12.1.0.2.0.zip

instantclient-sdk-linux.x64-12.1.0.2.0.zip

下载地址:https://www.oracle.com/technetwork/cn/database/linuxx86-64soft-095635-zhs.html

如何下载及安装,不再讲述。

验证:

[python@master ora]$ python3

Python 3.7.0 (default, Sep 15 2018, 17:37:08)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle

>>>

python连接oracle的方式,这里讲述两种:

(1)一个字符串完成

import cx_Oracle

#建立和数据库系统的连接

conn = cx_Oracle.connect('C##scott/[email protected]:1521/orcl')

#获取操作游标

cursor = conn.cursor()

#执行SQL,创建一个表

cursor.execute("""create table tb_user(id number, name varchar2(50),password varchar(50),primary key(id))""")

#关闭连接,释放资源

cursor.close()

#执行完成,打印提示信息

print ('Completed!')

(2)分开三个字符串

import cx_Oracle

#建立和数据库系统的连接

db=cx_Oracle.connect('c##scott','tiger','192.168.1.200:1521/orcl')

#获取操作游标

cursor = db.cursor()

#执行SQL,创建一个表

cursor.execute("""insert into tb_user values(1,'scott','tiger')""")

#关闭连接,释放资源

cursor.close()

#执行完成,打印提示信息

db.commit()

db.close()

print ('Completed!')

对数据库的操作:

#在Oracle插入数据

import cx_Oracle

conn= cx_Oracle.connect('C##scott/[email protected]:1521/orcl')

cursor=conn.cursor()

#插入一条记录

cursor.execute("""insert into tb_user values(1,'admin','password')""");

#再插入一条数据

param={'id':2,'n':'admin','p':'password'}

cursor.execute('insert into tb_user values(:id,:n,:p)',param);

#一次插入多条数据,参数为字典列表形式

param=[{'id':3,'n':'admin','p':'password'},{'id':4,'n':'admin','p':'password'},{'id':5,'n':'admin','p':'password'}];

cursor.executemany('insert into tb_user values(:id,:n,:p)',param);

#再一次插入多条数据

param=[];

#生成5条插入数据,参数为元组列表形式for i in range(6,11): # [6,7,8,9,10]

param.append((i,'user'+str(i),'password'+str(i)))

#插入数据

cursor.executemany('insert into tb_user values(:1,:2,:3)',param);

cursor.close();

#提交更改

conn.commit();

conn.close();

#在Oracle中查询数据

import cx_Oracle

conn= cx_Oracle.connect('C##scott/[email protected]:1521/orcl')

cursor=conn.cursor()

#执行查询 语句

cursor.execute("""select * from tb_user""")

#获取一条记录

one=cursor.fetchone()

print ('1: id:%s,name:%s,password:%s'%one)

#获取两条记录!!!注意游标已经到了第二条

two= cursor.fetchmany(2)

print ('2 and 3:',two[0],two[1])

#获取其余记录!!!注意游标已经到了第四条

three=cursor.fetchall();for row inthree:

print (row) #打印所有结果

print ('条件查询')

cursor.prepare("""select * from tb_user where id <= :id""")

cursor.execute(None,{'id':5})for row incursor: #相当于fetchall()

print (row)

cursor.close();

conn.close();

[python@master ora]$ python3 select_ora.py

1: id:1,name:admin,password:password

2 and 3: (2, 'admin', 'password') (3, 'admin', 'password')

(4, 'admin', 'password')

(5, 'admin', 'password')

(6, 'user6', 'password6')

(7, 'user7', 'password7')

(8, 'user8', 'password8')

(9, 'user9', 'password9')

(10, 'user10', 'password10')

条件查询

(1, 'admin', 'password')

(2, 'admin', 'password')

(3, 'admin', 'password')

(4, 'admin', 'password')

(5, 'admin', 'password')

你可能感兴趣的:(python连接oracle查询数据_python连接oracle数据库)