Python in Ubuntu accesses the Oracle database through the SQLAlchemy Library
SQLAlchemy is not a database driver. The Python driver for accessing Oracle data is cx_Oracle, but this driver depends on Oracle instant client. Therefore, if you want to use the SQLAlchemy library to access the Oracle database, you must first install the Oracle instant client, then install cx_Oracle, and finally install SQLAlchemy.
After a lap, I found that the process was cumbersome and the current information was somewhat outdated. I wrote a document to share with you.
Install the Oracle instant client and download the installation package from the Oracle official website.
Http://www.oracle.com/technet...
I see the version 12.1.0.2.0. There are many packages in the same version. To support cx_Oracle, You need to download the following two packages.
Oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
Oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
Basic and devel packages for short
Basic contains all oracle client lib/bin files
Devel contains the header file of the oracle client external interface
Install
Oracle only provides RPM packages. my operating system is Ubuntu and I need to install an alien for conversion.
Sudo install alien to complete installation
Use alien to convert an RPM package into a DEB package,
sudo alien -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm sudo alien -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
Then install the deb package
sudo dpkg -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.deb sudo dpkg -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.deb
After the basic package is successfully installed, copy the lib and bin libraries in the/usr/lib/oracle/12.1/client64/directory, after the devel package is installed, the include header file will be copied in the/usr/lib/oracle/12.1/client64/directory.
However, I failed to use alien to convert the devel RPM package. Then I decompressed the RPM package and found that the devel RPM package contains some include files, copy these files to the/usr/lib/oracle/12.1/client64/include directory.
The installation package stores the bin, lib, and include header files in the specified directory. If the package cannot be installed, decompress the package and run it on your own.
Set Environment Variables
The purpose of setting environment variables is to allow other programs to find the installed Oracle database.
$ export ORACLE_HOME=/usr/lib/oracle/11.2/client64 $ export LD_LIBRARY_PATH=$ORACLE_HOME/lib
Link oracle database to system directory
Create the oracle. conf file in the/etc/ld. so. conf. d directory and specify the path of the oracle database/usr/lib/oracle/11.2/client64/lib in the file.
Execute Link Configuration
$ sudo ldconfig
Install cx_Oracle
Install the libaio package. libaio is an asynchronous non-blocking interface in Linux. It provides an asynchronous non-blocking method to read and write files. The read and write efficiency is relatively high, and the oracle SQLPlus library depends on it.
sudo apt-get install libaio1
After all preparations are completed, run the package installation command to successfully install cx_Oracle.
sudo apt-get install cx_Oracle
If preparations are not completed, various installation failures will be reported. You can compare the installation steps to check whether the preceding operations are correct.
Install sqlalchemy
Use sudo apt-get install sqlalchemy to complete the installation.