1、安装cx_Oracle包:
http://cx-oracle.sourceforge.net/需要注意下版本,根据操作系统和已安装的python版本进行选择
在命令窗口中执行(要先设置Python的环境变量):
python -m pip install cx_Oracle --upgrade
2、Python连接oracle数据库的基本操作:
(1)创建数据库连接connect和关闭数据库连接close,创建数据库连接的三种方式:
方法一:用户名、密码和监听分开写
import cx_Oracle
db=cx_Oracle.connect(‘username/password@localhost:1521/orcl‘)
db.close()
方法二:用户名、密码和监听写在一起
import cx_Oracle
db=cx_Oracle.connect(‘username‘,‘password‘,‘localhost:1521/orcl‘)
db.close()
方法三:配置监听并连接(推荐使用)
import cx_Oracle
tns=cx_Oracle.makedsn(‘localhost‘,1521,‘orcl‘)
db=cx_Oracle.connect(‘username‘,‘password‘,tns)
db.close()
(2)建立cursor并执行SQL语句(查询、更新、插入、删除):创建数据库连接,创建游标cursor,然后执行sql语句,执行完成后,关闭游标,关闭数据库连接
<