零零散散我爱编程

Windows 下 python3 连接oracle 数据库

2018-04-03  本文已影响38人  南北VS东西

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()
上一篇下一篇

猜你喜欢

热点阅读