Windows 下 python3 连接oracle 数据库

Windows下本地安装Oracle 11g、 Navicat等数据库工具

资源数据库资源和安装文档
python 中 进行Oracle数据库连接和测试
step1:安装cx_Oracle包

http://cx-oracle.sourceforge.net/ 需要注意下版本,根据操作系统和已安装的python版本进行选择

输入图片说明

#使用的时候 
import cx_Oracle as cx  #(cx)方便自己用 起的别名

step2 连接数据库

创建数据库连接的三种方式:

方法一:用户名、密码和监听分开写

import cx_Oracle

db=cx_Oracle.connect('username/password@host/orcl')

db.close()

方法二:用户名、密码和监听写在一起

import cx_Oracle

db=cx_Oracle.connect('username','password','host/orcl')

db.close()

方法三:配置监听并连接

import cx_Oracle

tns=cx_Oracle.makedsn('host',1521,'orcl')

db=cx_Oracle.connect('username','password',tns)

db.close()

step3 建立cursor并执行SQL语句:查询、更新、插入、删除

查询

conn = cx.connect('username/password@localhost/ORCL')
cursor = conn.cursor()
cursor.execute("select * from DEPT")
row = cursor.fetchone()
print(row)
cursor.close()
conn.close()

插入

conn = cx.connect('username/password@localhost/ORCL')
cursor = conn.cursor()
cursor.execute("INSERT INTO SCOTT.SF VALUES(" + "'" + point_code + "'," + "'" + name + "'," + "'" + address + "'," + "'" + tel + "')")
row = cursor.fetchone()
print(row)
cursor.close()
conn.close()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容