oracle 将python连接到oracle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8266568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect python to oracle
提问by kostas
I have installed oracle client and oracle client-dev, and setup ORACLE_HOME environment variable. Yet when I attempt to install tux_oracle (python setup.py build) I get the following: fatal error: oci.h: No such file or directory
我已经安装了 oracle client 和 oracle client-dev,并设置了 ORACLE_HOME 环境变量。然而,当我尝试安装 tux_oracle (python setup.py build) 时,我得到以下信息:fatal error: oci.h: No such file or directory
回答by kostas
Well, the answer is to install cx_Oracle not tx_Oracle. The way to do it in Ubuntu is the following:
嗯,答案是安装 cx_Oracle 而不是 tx_Oracle。在 Ubuntu 中执行此操作的方法如下:
- You must have the oracle client installed in your PC. If not download from oracle http://www.oracle.com/technetwork/topics/linuxsoft-082809.html.
- Install alien in your PC to be able to convert rpm to deb:
- 您必须在您的 PC 上安装 oracle 客户端。如果不是从 oracle http://www.oracle.com/technetwork/topics/linuxsoft-082809.html下载。
- 在您的 PC 中安装外星人,以便能够将 rpm 转换为 deb:
sudo apt-get -i alien
- Convert the rpm to deb:
- 将 rpm 转换为 deb:
sudo alien -d oracle-instantclient11.2-sqlplus-11.2.0.2.0.i386.rpm
- Install
sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.2.0-2_i386.deb
- Set ORACLE_HOME environment variable in /etc/profile.d by creating a file oracle.sh which must contain:
- 安装
sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.2.0-2_i386.deb
- 通过创建文件 oracle.sh 在 /etc/profile.d 中设置 ORACLE_HOME 环境变量,该文件必须包含:
export ORACLE_HOME=/usr/lib/oracle/11.2/client source oracle.sh
- Update LD_LIBRARY_PATH:
- 更新 LD_LIBRARY_PATH:
sudo vi /etc/ld.so.conf.d/oracle.conf which must contain: "$ORACLE_HOME/lib" sudo ldconfig
- Download and install cx_Oracle (depending on the version of oracle and python you are using) from http://cx-oracle.sourceforge.net/Again convert rpm to deb using alien and install:
- 从http://cx-oracle.sourceforge.net/下载并安装 cx_Oracle(取决于您使用的 oracle 和 python 版本) 再次使用Alien 将 rpm 转换为 deb 并安装:
sudo alien -d cx_Oracle-5.1-11g-py32-1.i386.rpm sudo dpkg -i cx-oracle_5.1-2_i386.deb
- cx_Oracle gets installed in /usr/lib/python2.7/dist-packages/. You must do: sudo mv site-packages/cx_Oracle* dist-packages/ sudo rmdir site-packages/ sudo ln -s dist-packages site-packages
- cx_Oracle 安装在 /usr/lib/python2.7/dist-packages/ 中。你必须这样做: sudo mv site-packages/cx_Oracle* dist-packages/ sudo rmdir site-packages/ sudo ln -s dist-packages site-packages
Now you should have no problem connecting to oracle. From python type:
现在连接到oracle 应该没有问题了。从 python 类型:
import cx_Oracle
To connect to the database specify a connection string in tsnames.ora or directly:
要连接到数据库,请在 tsnames.ora 中或直接指定连接字符串:
connection_string = 'username/password@(DESCRIPTION= (ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA= (SID=MY_SID)))'
回答by gecco
回答by thoaionline
That header file can be found in the development suite. For some reason, it's not included in the default distribution package. Have a look at
该头文件可以在开发套件中找到。出于某种原因,它不包含在默认分发包中。看一下
http://my.opera.com/onyxluo/blog/cannot-find-oci-h-in-oracle9-2-client
http://my.opera.com/onyxluo/blog/cannot-find-oci-h-in-oracle9-2-client
Here's the page content, for your convenience. I got it from Google cache as the page was down when I got there.
这是页面内容,为您提供方便。当我到达那里时页面已关闭,因此我从 Google 缓存中获取了它。
The reason of this problem is because OCI (Oracle Call Interface)package is not installed in Oracle9.2 client. The default path of "oci.h" is $ORACLE_HOME/rdbms/demo. This problem doesn't exist on Oracle Database 9.2.0.1. But for Oracle 9.2.0.1 client, OCI package is not included in client even if you select the full package of client installation.
Solution:
- install Oracle 9.2.0.1 client first.
- In OUI(Oracle Universal Installer), use the same oracle home with Oracle 9.2.0.1 client, and then select Oracle Database install.
- Choose Customized in database installation
- Uncheck Enterprise Manager and Oracle Database and others except OCI and OCCI.
After OCI installed, $ORACLE_HOME/rdbms/demo will contain oci.h and other *.h files.
出现这个问题的原因是Oracle9.2客户端没有安装OCI(Oracle Call Interface)包。“oci.h”的默认路径是$ORACLE_HOME/rdbms/demo。这个问题在 Oracle 数据库 9.2.0.1 上不存在。但是对于 Oracle 9.2.0.1 客户端,即使选择完整的客户端安装包,客户端也不包含 OCI 包。
解决方案:
- 首先安装 Oracle 9.2.0.1 客户端。
- 在OUI(Oracle Universal Installer)中,使用与Oracle 9.2.0.1客户端相同的oracle home,然后选择Oracle Database install。
- 在数据库安装中选择自定义
- 取消选中 Enterprise Manager 和 Oracle Database 以及除 OCI 和 OCCI 之外的其他选项。
OCI 安装后,$ORACLE_HOME/rdbms/demo 将包含 oci.h 和其他 *.h 文件。