Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect Python to Oracle

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to achieve Python connection Oracle". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One: install Python 3.6,

Environment settin

Linux:Centos 6.7

Python:Python 3.6

DB:Oracle 19C

New oracle users

The name can be taken at will, and the script can be put under the user.

[root@jumplinux01 ~] # useradd oracle

[root@jumplinux01 ~] # passwd oracle

Create a new installation directory

[root@jumplinux01 ~] # su-oracle

[oracle@jumplinux01 ~] $mkdir softwore

[oracle@jumplinux01 ~] $cd softwore/

[oracle@jumplinux01 softwore] $

Install Python3.6

1. Since the version of Python is 2.6 after the system is installed, you need to upgrade to 3.6 first.

[oracle@jumplinux01 softwore] $python

Python 2.6.6 (r266 84292, Jul 23 2015, 15:22:56)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

> > >

Download the installation file and extract it

[oracle@jumplinux01 softwore] $wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

[oracle@jumplinux01 softwore] $ll-rth

Total 22M

-rw-rw-r-- 1 oracle oracle 22m Mar 21 2017 Python-3.6.1.tgz

[oracle@jumplinux01 softwore] $tar-zxvf Python-3.6.1.tgz

[oracle@jumplinux01 softwore] $cd Python-3.6.1

Create a Python installation directory

[root@jumplinux01 ~] # mkdir / usr/local/python36

[root@jumplinux01] # chown oracle.oracle / usr/local/python36-R

[root@jumplinux01 ~] # su-oracle

[oracle@jumplinux01 ~] $cd softwore/Python-3.6.1

[oracle@jumplinux01 Python-3.6.1] $. / configure-- prefix=/usr/local/python36

[oracle@jumplinux01 Python-3.6.1] $make

[oracle@jumplinux01 Python-3.6.1] $make install

Modify link

[root@jumplinux01 ~] # mv / usr/bin/python / usr/bin/python_old

[root@jumplinux01] # ln-s / usr/local/python36/bin/python3.6 / usr/bin/python

[root@jumplinux01 ~] # su-oracle

Verify that the installation is successful

[oracle@jumplinux01 ~] $python

Python 3.6.1 (default, Mar 16 2020, 21:33:03)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux

Type "help", "copyright", "credits" or "license" for more information.

> > >

Yum problem handling

After the upgrade, yum will not be able to use. Then you need to modify the yum executable file.

Question:

[root@jumplinux01 ~] # yum list

File "/ usr/bin/yum", line 30

Except KeyboardInterrupt, e:

^

SyntaxError: invalid syntax

Deal with:

[root@jumplinux01 ~] # vim / usr/bin/yum

#! / usr/bin/python2.6

[root@jumplinux01 ~] # yum list

Loaded plugins: fastestmirror

Determining fastest mirrors

* base: mirrors.aliyun.com

* extras: mirrors.aliyun.com

* updates: mirrors.aliyun.com

...

II: installation of cx_Oracle module

This section includes two parts: the installation of oracle client and the installation of cx_Oracle module.

Environment settin

The Linux system is Centos 6.7.

Python environment is Python 3.6

Oracle module: cx_Oracle

Oracle client: Oracle 12c Instant Client

1. Install Oracle 12c Instant Client

Since we use Python to connect to Oracle this time, we need an oracle client, here we use Oracle 12c Instant Client

Https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

[root@jumplinux01] # chown oracle.oracle / home/oracle/softwore-R

[root@jumplinux01 ~] # ll-rth / home/oracle/softwore/

Total 73M

-rw-rw-r-- 1 oracle oracle 22m Mar 21 2017 Python-3.6.1.tgz

Drwxr-xr-x 18 oracle oracle 4.0K Mar 16 21:34 Python-3.6.1

-rw-r--r-- 1 oracle oracle 593K Mar 16 22:15 oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm

-rw-r--r-- 1 oracle oracle 51m Mar 16 22:15 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm

1.1 install the Oracle client (root user)

[root@jumplinux01 softwore] # rpm-ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm

[root@jumplinux01 softwore] # rpm-ivh oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm

1.2 add ORACLE_HOME user environment variable (root user)

[root@jumplinux01 softwore] # vi ~ / .bash_profile

Export ORACLE_HOME=/usr/lib/oracle/12.2/client64

Export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib

[root@jumplinux01 softwore] # source ~ / .bash_profile

two。 Download the cx_Oracle module

A module dedicated to connecting Oracle data

Https://pypi.org/project/cx-Oracle/5.2.1/#downloads

[root@jumplinux01 softwore] # ll-rth cx_Oracle-5.2.1.tar.gz

-rw-r--r-- 1 oracle oracle 111K Mar 16 22:23 cx_Oracle-5.2.1.tar.gz

3. Install the cx_Oracle module (root user)

[oracle@jumplinux01 softwore] $tar-zxvf cx_Oracle-5.2.1.tar.gz

[oracle@jumplinux01 softwore] $cd cx_Oracle-5.2.1

[oracle@jumplinux01 cx_Oracle-5.2.1] $python setup.py build

[oracle@jumplinux01 cx_Oracle-5.2.1] $python setup.py install

...

Installed / usr/local/python36/lib/python3.6/site-packages/cx_Oracle-5.2.1-py3.6-linux-x86_64.egg

Processing dependencies for cx-Oracle==5.2.1

Finished processing dependencies for cx-Oracle==5.2.1

Verify installation

If there is no error in import, the installation is successful.

[oracle@jumplinux01 cx_Oracle-5.2.1] $python

Python 3.6.1 (default, Mar 16 2020, 21:33:03)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux

Type "help", "copyright", "credits" or "license" for more information.

> import cx_Oracle

> > >

Three: use cx_Oracle to connect to Oracle (basic article)

[oracle@jumplinux01 softwore] $vi connectoracle.py

#! / usr/bin/python

# coding=utf8

# Import cx_Oracle module

Import cx_Oracle

# create a connection to the Oracle database and assign it to variables

Db=cx_Oracle.connect ('cjc/cjc@192.168.2.222:1521/cjcpdb01')

# create a cursor and assign it to the variable cursor

Cursor=db.cursor ()

# execute Oracle SQL statement

Cursor.execute ('select sysdate from dual')

# get the execution result and assign it to the variable data

# here fetchone means to get a row, and fetchall means to get all the rows

# fetchone returns a string

# fetchall returns a list, even if the result is only one line

Data=cursor.fetchone ()

# print the result

Print ('Database time:% s'% data)

# close database connection

Cursor.close ()

Db.close ()

Running result:

Error:

[root@jumplinux01 ~] # su-oracle

[oracle@jumplinux01 ~] $cd softwore/

[oracle@jumplinux01 softwore] $python connectoracle.py

Traceback (most recent call last):

File "connectoracle.py", line 8, in

Db=cx_Oracle.connect ('cjc/cjc@192.168.2.222:1521/cjcpdb01')

Cx_Oracle.DatabaseError: ORA-24454: client host name is not set

Solution: add hosts

[root@jumplinux01 ~] # cat / etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

:: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.2.221 jumplinux01

192.168.2.222 cjcos

Run the result again:

[oracle@jumplinux01 softwore] $python connectoracle.py

"how to achieve Python connection Oracle" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report