`
txf2004
  • 浏览: 6876589 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python 连接 Oracle 示例

 
阅读更多

二. Python 连Oracle 的基本操作

2.1 DB连接和关闭DB连接

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

[root@rac1 u01]# cat db.py

import cx_Oracle

db=cx_Oracle.connect('system','oracle','192.168.2.42:1521/dave')

print db.version

db.close()

[root@rac1 u01]# python db.py

10.2.0.1.0

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

[root@rac1 u01]# cat db.py

import cx_Oracle

db=cx_Oracle.connect('system/oracle@192.168.2.42:1521/dave')

print db.version

db.close()

[root@rac1 u01]# python db.py

10.2.0.1.0

2.1.3 方法三:配置监听并连接

[root@rac1 u01]# cat db.py

import cx_Oracle

tns=cx_Oracle.makedsn('rac1',1521,'dave1')

db=cx_Oracle.connect('system','oracle',tns)

print tns

print db.version

vs=db.version.split('.')

print vs

if vs[0]=='10':

print "This is Oracle 10g!"

db.close()

[root@rac1 u01]# python db.py

(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=rac1)(PORT=1521)))(CONNECT_DATA=(SID=dave1)))

10.2.0.1.0

['10', '2', '0', '1', '0']

This is Oracle 10g!

2.2 建立cursor 并执行SQL语句

[root@rac1 u01]# cat db.py

import cx_Oracle

tns=cx_Oracle.makedsn('rac1',1521,'dave1')

db=cx_Oracle.connect('system','oracle',tns) --创建连接

cr=db.cursor() --创建cursor

sql='select * from phone'

cr.execute(sql) --执行sql 语句

print "\nThis is Fetchall!"

rs=cr.fetchall() --一次返回所有结果集

print "print all:(%s)" %rs

print "\n print by row:"

for x in rs:

print x

print "\nThis is Fetone!"

cr.execute(sql)

while(1):

rs=cr.fetchone() --一次返回一行

if rs ==None:break

print rs

--使用参数查询

print "\n select with parameter:"

pr={'id':3,'tel':13888888888}

cr.execute('select * from phone where id=:id or phone=:tel',pr)

--这里我们将参数作为一个字典来处理的

rs=cr.fetchall()

print rs

cr.execute('select * from phone where id=:myid or phone=:myphone',myid=2,myphone=13888888888)

--这里我们直接写参数

rs=cr.fetchall()

print rs

cr.close()

db.close()

[root@rac1 u01]# python db.py

This is Fetchall!

print all:([(1, 13865999999L), (2, 13888888888L)])

print by row:

(1, 13865999999L)

(2, 13888888888L)

This is Fetone!

(1, 13865999999L)

(2, 13888888888L)

select with parameter:

[(2, 13888888888L)]

[(2, 13888888888L)]

Python 类型和Oracle 类型的对应关系:

During the fetch stage, basic Oracle data types get mapped into their Python equivalents. cx_Oracle maintains a separate set of data types that helps in this transition. The Oracle - cx_Oracle - Python mappings are:

Oracle

cx_Oracle

Python

VARCHAR2
NVARCHAR2
LONG

cx_Oracle.STRING

str

CHAR

cx_Oracle.FIXED_CHAR

NUMBER

cx_Oracle.NUMBER

int

FLOAT

float

DATE

cx_Oracle.DATETIME

datetime.datetime

TIMESTAMP

cx_Oracle.TIMESTAMP

CLOB

cx_Oracle.CLOB

cx_Oracle.LOB

三. 一个完成的示例

在这个例子里,我们将用Python 对DB 进行一些操作,包括,创建一张表,并插入一些数据,在修改其中的部分数据。

[root@rac1 u01]# cat dave.py

#!/usr/bin/python

#coding=utf-8

import cx_Oracle

import sys

import urllib

import os

def connectDB(dbname='dave'):

if dbname=='dave':

connstr='system/Oracle@192.168.2.42:1521/dave'

db=cx_Oracle.connect(connstr)

return db

def sqlSelect(sql,db):

#include:select

cr=db.cursor()

cr.execute(sql)

rs=cr.fetchall()

cr.close()

return rs

def sqlDML(sql,db):

#include: insert,update,delete

cr=db.cursor()

cr.execute(sql)

cr.close()

db.commit()

def sqlDML2(sql,params,db):

# execute dml with parameters

cr=db.cursor()

cr.execute(sql,params)

cr.close()

db.commit()

def sqlDDL(sql,db):

#include: create

cr=db.cursor()

cr.execute(sql)

cr.close()

if __name__=='__main__':

print "This is a test python program,write by tianlesoftware!\n"

os.environ['NLS_LANG']='SIMPLIFIED CHINESE_CHINA.UTF8'

#connect to database:

db=connectDB()

#create a table:

sql='create table dave(id number,name varchar2(20),phone number)'

sqlDDL(sql,db)

#insert data to table dave:

sql='insert into dave values(1,\'tianlesoftware\',13888888888)'

sqlDML(sql,db)

dt=[{'id':2,'name':'dave','phone':138888888888},

{'id':3,'name':'Oracle','phone':13888888888},

{'id':4,'name':'anqing','phone':13888888888}]

sql='insert into dave values(:id,:name,:phone)'

for x in dt:

sqlDML2(sql,x,db)

#select the result:

print "this is the first time select the data from dave"

sql='select * from dave'

rs=sqlSelect(sql,db)

for x in rs:

print x

#update data where id=1,change the name to anhui

sql='update dave set name=\'anhui\' where id=1'

sqlDML(sql,db)

#select again:

print "\n change the nanme to anhui where id equal 1,and select the result"

sql='select * from dave'

rs=sqlSelect(sql,db)

for x in rs:

print x

#delete data where id=3

sql='delete from dave where id=3'

sqlDML(sql,db)

#select again:

print "\n delete the data where id equal 3 and select the result"

sql='select * from dave'

rs=sqlSelect(sql,db)

for x in rs:

print x

db.close()

[root@rac1 u01]# python dave.py

This is a test python program,write by tianlesoftware!

this is the first time select the data from dave

(1, 'tianlesoftware', 13888888888L)

(2, 'dave', 138888888888L)

(3, 'Oracle', 13888888888L)

(4, 'anqing', 13888888888L)

change the nanme to anhui where id equal 1,and select the result

(1, 'anhui', 13888888888L)

(2, 'dave', 138888888888L)

(3, 'Oracle', 13888888888L)

(4, 'anqing', 13888888888L)

delete the data where id equal 3 and select the result

(1, 'anhui', 13888888888L)

(2, 'dave', 138888888888L)

(4, 'anqing', 13888888888L)

关于Python 连接Oracle 数据库,及一些基本操作,就这么多。

分享到:
评论

相关推荐

    利用python-oracledb库连接Oracledb数据库,使用示例

    python-oracledb的源码和使用示例代码, python-oracledb 1.0,适用于Python versions 3.6 through 3.10. Oracle Database; This directory contains samples for python-oracledb. 1. The schemas and SQL ...

    Python使用cx_Oracle调用Oracle存储过程的方法示例

    本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下: 这里主要测试在Python中通过cx_Oracle调用PL/SQL。 首先,在数据库端创建简单的存储过程。 create or replace ...

    Python-连接-Oracle-示例.doc

    Python-连接-Oracle-示例.doc

    windows下python连接oracle数据库

    python连接oracle数据库的方法,具体如下 1.首先安装cx_Oracle包 2.解压instantclient-basic-windows.x64-...python连接示例代码: # -*- coding: utf-8 -*- import cx_Oracle conn=cx_Oracle.connect('reporter','pas

    基于python连接oracle导并出数据文件

    主要介绍了基于python连接oracle导并出数据文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    python链接Oracle数据库的方法

    本文实例讲述了python链接Oracle数据库... '''Hello cx_Oracle示例: 1)打印数据库版本信息. 2)查询表数据.''' conn = cx_Oracle.connect("obs61","obs61","tx8i.hp") cur = conn.cursor() try: print "Oracle Ve

    Python编程实战之Oracle数据库操作示例

    本文实例讲述了Python编程实战之Oracle数据库操作。分享给大家供大家参考,具体如下: 1. 要想使Python可以操作Oracle数据库,首先需要安装cx_Oracle包,可以通过下面的地址来获取安装包 ...

    Python读写及备份oracle数据库操作示例

    主要介绍了Python读写及备份oracle数据库操作,结合实例形式分析了Python针对Oracle数据库操作的相关库安装,以及使用cx_Oracle与pandas库进行Oracle数据库的查询、插入、备份等操作相关实现技巧,需要的朋友可以参考下

    cx_Oracle-7.1.2-cp37-cp37m-win_amd64.zip

    使用python语言连接oracle数据库的连接工具,示例如下: import cx_Oracle connection = cx_Oracle.connect("scott", "tiger", "localhost/orcl") cursor = connection.cursor() cursor.execute(""" SELECT empno...

    python实现Oracle查询分组的方法示例

    主要介绍了python实现Oracle查询分组的方法,结合实例形式分析了python使用group by子句及having子句实现Oracle查询分组的相关操作技巧,需要的朋友可以参考下

    python使用 cx_Oracle 模块进行查询操作示例

    主要介绍了python使用 cx_Oracle 模块进行查询操作,结合实例形式分析了Python使用cx_Oracle模块进行数据库的基本连接、查询、输出等相关操作技巧,需要的朋友可以参考下

    oracle-db-examples:Oracle数据库的应用程序和工具使用示例

    oracle-db-examples 该存储库存储了许多示例,这些...基于Python的示例 基于Ruby的示例 空间示例 基于SQL的示例 示例 文献资料 您可以在下找到Oracle数据库的联机文档。 实时SQL 您可以在基于Web的免费工具执行在此

    Python使用sqlalchemy模块连接数据库操作示例

    本文实例讲述了Python使用sqlalchemy模块连接数据库操作。分享给大家供大家参考,具体如下: 安装: pip install sqlalchemy # 安装数据库驱动: pip install pymysql pip install cx_oracle 举例:(在url后面...

    Python-Helidon是一个Oracle开源运行在Netty内核上的Java微服务框架

    轻量级且快速:Helidon 旨在设计得简单易用,配有工具和示例,可帮助你快速上手。支持 Microprofile:Helidon 支持 MicroProfile 并提供熟悉的 API,如 JAX-RS, CDI 和 JSON-P/B。Helidon 的 MicroProfile 实现在 ...

    LDP_Protocols:Python中的示例LDP实现

    规约OLH 频率Oracle(原始估计直方图) 相关文章:方波密度Oracle(用于数字/标准值) 相关文章:澄清:引用33应该是Ning Wang等。 收集和分析具有局部差异隐私的多维数据。 ICDE 2019。SVSM LDP下的频繁项集挖掘...

Global site tag (gtag.js) - Google Analytics