includes: Oracle Database in Windows installation and configuration under the operating system, python The third-party expansion packs that need to be installed, as well as sample learning for basic operations.
- Oracle Database
- Oracle Environment Configuration & Client Connectivity
- Download Installation Oracle Green Edition Client instantclient :
to the Oracle Download website instantclient Basic package, unzip to E:\ProgramFile\instantclient ;
- Set 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
- Configuration Tnsnames.tns File:
in the E:\ProgramFile\instantclient file creation under Tnsnames.ora , 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.
- Configuration PL/SQL Developer to connect:
in tools/preference oracle-connection option configuration oracle home and oci library :
- Oracle Home : E:\ProgramFile\instantclient
- OCI Library : E:\ProgramFile\instantclient\oci.dll
configuration complete, can be restarted Plsql Developer , enter the correct username and password to connect ~~~
- pyth On connection oracle database
- install cx_oracle package:
http://cx-oracle.sourceforge.net/ Need to be aware of the next version, choose according to the operating system and the version of python installed
Choose your own version and execute it directly after download
- Post-Installation verification:
Import cx_oracle No error:
Note: if import cx_oracle times error prompt cannot find oci. DLL , Workaround: To install oracle Find one on the machine, and then copy to libsite-packages The directory can be.
- Python Connection Oracle Basic operations of the database
- Create 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 ()
- build cursor sql statement: query, UPDATE, INSERT, delete
- Create a database connection, create a cursor cursor sql statement, when execution is complete, close the cursor and close the database connection
After creating the connection, establish cursor , and perform SQL Statement
- submit after insert, UPDATE, delete operation Commit
This article mainly introduces Python's operational learning of Oracle Database