草庐IT

Python安装cx_oracle操作

快乐女孩824 2023-04-13 原文

1.cx_Oracle概述

cx_Oracle是一个Python 扩展模块,通过使用所有数据库访问模块通用的数据库 API来实现 Oracle 数据库的查询和更新。为使用一些专为 Oracle 设计的特性,还加入了多个通用数据库 API 的扩展。cx_Oracle 的开发历时多年,涵盖了大多数需要在 Python 中访问 Oracle 的客户的需求。

安装cx_Oracle使用pip命令: pip install cx_Oracle

2.连接oracle数据库

cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

cx_Oracle.connect('scott/scott@127.0.0.1:1521/orcl')

3.ORACLE的查询

#简单查询

import cx_Oracle

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

sql_cmd='SELECT * FROM students'

db_cursor.execute(sql_cmd)

for row in  db_cursor:

    print(row)

db_cursor.close()

db_conn.close()

#带参数查询

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

sql_cmd='SELECT * FROM students where ID = :id'

sql_p_id={'id':2}

db_cursor.execute(sql_cmd,sql_p_id)

for row in  db_cursor:

    print(row)

db_cursor.close()

db_conn.close()

#获取单行可以使用fetchone函数;获取多行记录,可以使用fetchall函数

import cx_Oracle

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

sql_cmd='SELECT * FROM students'

db_cursor.execute(sql_cmd)

print(db_cursor.fetchone())

db_cursor.close()

db_conn.close()

4.ORACLE的DML

import cx_Oracle

from datetime import datetime

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

sql_cmd = 'INSERT INTO students(id, name, age,birth) VALUES(:id, :name, :age,:birth)'

db_cursor.execute(sql_cmd,(11,'王五2',12,datetime(2017,9,1,12,40,12)))

db_cursor.execute(sql_cmd,(22,'赵六',12,datetime(2017,9,1,12,40,12)))

db_conn.commit()

db_cursor.close()

db_conn.close()

import cx_Oracle

from datetime import datetime

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

sql_cmd = 'INSERT INTO students(id, name, age,birth) VALUES(:id, :name, :age,:birth)'

db_cursor.executemany(sql_cmd, [(15, '王五2', 12, datetime(2017, 9, 1, 12, 40, 12)),(16, '赵六', 12, datetime(2017, 9, 1, 12, 40, 12))])

db_conn.commit()

db_cursor.close()

db_conn.close()

5.调用存储过程和函数

--存储过程代码:

CREATE OR REPLACE PROCEDURE P_DEMO(V1 IN VARCHAR2, V2 OUT VARCHAR2) IS

BEGIN

  V2 := V1;

END;

  

#Python代码:

import cx_Oracle

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

str='abdd'

msg =db_cursor.var(cx_Oracle.STRING)

db_cursor.callproc('p_demo',[str,msg])

db_conn.commit()

print(msg)

print(msg.getvalue())

db_cursor.close()

db_conn.close()

 

--函数代码:

CREATE OR REPLACE function F_DEMO(V1 VARCHAR2,V2 VARCHAR2) RETURN VARCHAR2

IS

BEGIN

  RETURN V1 || v2;

END;

#Python代码:

import cx_Oracle

db_conn = cx_Oracle.connect('scott', 'scott', '127.0.0.1:1521/orcl')

db_cursor=db_conn.cursor()

str=db_cursor.callfunc('f_demo',cx_Oracle.STRING,['abc','ddd'])

print(str)

db_conn.commit()

db_cursor.close()

db_conn.close()

有关Python安装cx_oracle操作的更多相关文章

  1. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  8. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  9. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  10. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

随机推荐