欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python通过cx_oracle操作数据库过程简单记录

程序员文章站 2023-09-20 17:02:36
1、环境配置 环境配置过程中,需要关注软件版本是否一致,主要包括:oracle客户端版本、cx_oracle版本、python版本; 2、操作记录 (1)验证环境是否正常;(无报错即为正常) import cx_Oracle (2)创建数据库连接,方式大致三种; db1=cx_Oracle.conn ......

1、环境配置

  环境配置过程中,需要关注软件版本是否一致,主要包括:oracle客户端版本、cx_oracle版本、python版本;

2、操作记录

  (1)验证环境是否正常;(无报错即为正常)

  import cx_oracle

  (2)创建数据库连接,方式大致三种;

  db1=cx_oracle.connect('user/password@host/orcl')

  db2=cx_oracle.connect('user','password','host/orcl')

  tnsname=cx_oracle.makedsn('host',1521,'orcl')

  db3=cx_oracle.connect('user','password',tnsname)

  (3)关闭数据库;

  db.close()

  (4)查询

  cr=db.cursor()  #创建游标

  cr.execute ("select * from  pub_sysinit where initname like '%消耗%'")  #sql

  cr.fetchall()  #获取全部

  cr.fetchone()  #逐行获取,每次一条