Python Operations Oracle Database

Source: Internet
Author: User

This article mainly introduces Python's operational learning of Oracle Database

Includes: Installation and configuration of Oracle database under Windows operating system, third-party expansion packages that Python needs to install, and sample learning for basic operations.

1 Oracle Database 1.1 Oracle Environment Configuration & Client Connection 1.1.1 Download install Oracle Green client instantclient:

Download Instantclient Basic Package to Oracle website and unzip to E:\ProgramFile\instantclient;

1.1.2 Setting Environment variables:
    • Nls_lang=simplified Chinese_china. ZHS16GBK (note that there are spaces in the middle, this if not set, Toad query Chinese will be garbled)
    • tns_admin= E:\ProgramFile\instantclient
    • Path= ...; E:\ProgramFile\instantclient
1.1.3 Configuration Tnsnames.tns File:

To create a file Tnsnames.ora under E:\ProgramFile\instantclient, paste the following into the file:

*** =

(DESCRIPTION =

(Address_list =

(ADDRESS = (PROTOCOL = TCP) (host= * * * *) (PORT = 1521))

)

(Connect_data =

(SERVER = dedicated)

(service_name = * *)

)

)

Note: The red part of the figure changes according to your needs.

1.1.4 Configure PL/SQL developer to connect:

In Tools/preference, the oracle-connection option configures the Oracle home and OCI Library:

    • Oracle Home:e:\programfile\instantclient
    • OCI Library:e:\programfile\instantclient\oci.dll

Configuration is complete, you can restart Plsql developer, enter the correct user name and password to connect ~ ~ ~

1.2 Python Connect Oracle Database 1.2.1 Install Cx_oracle package:

http://cx-oracle.sourceforge.net/need to be aware of the next version, depending on the operating system and the installed Python version to choose

Choose your own version and execute it directly after download

1.2.2 Post-Installation verification:

Import cx_oracle No error:

Note: If the import cx_oracle times wrong prompt cannot find OCI.DLL, workaround: Find one on the Oracle-loaded machine and copy it to the Libsite-packages directory.

1.3 Python basic operations for connecting to an Oracle database 1.3.1 creating a database connection connect and close the database connection close

There are three ways to create a database connection:

Method One: User name, password and listen separate write

Import Cx_oracle

Db=cx_oracle.connect (' Username/[email protected]/orcl ')

Db.close ()

Method Two: User name, password and monitoring are written together

Import Cx_oracle

Db=cx_oracle.connect (' username ', ' password ', ' HOST/ORCL ')

Db.close ()

Method Three: Configure the Listener and connect

Import Cx_oracle

TNS=CX_ORACLE.MAKEDSN (' Host ', 1521, ' ORCL ')

Db=cx_oracle.connect (' username ', ' password ', TNS)

Db.close ()

1.3.2 build cursor and Execute SQL statement: query, UPDATE, INSERT, delete 1.3.2.1 CREATE database connection, create cursor cursor, execute SQL statement, close cursor after execution completes, close the database connection

After creating the connection, build the cursor and execute the SQL statement

Commit commits after 1.3.2.2 insert, UPDATE, delete operation

Python Operations Oracle Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.