Python Oracle operations (cx_oracle)

Source: Internet
Author: User

Python can operate on an Oracle database by using the Cx_oracle module.
First, you need to download the Cx_oracle module: HTTPS://PYPI.PYTHON.ORG/PYPI/CX_ORACLE/6.0RC1

Note the version when downloading, against the Python version and the number of digits you are using.
I am using Python3.6, so the downloaded version is: CX_ORACLE-6.0RC1-CP36-CP36M-WIN_AMD64.WHL
You can then install:

python-m pip Install CX_ORACLE-6.0RC1-CP36-CP36M-WIN_AMD64.WHL

code example:

#用途: Operate Oracle database Demoimport Cx_oracleuser = "Yzwxceshi" passwd = "Yzwxceshi" listener = ' 192.168.20.191:1521/ORCL ' conn = cx _oracle.connect (user, passwd, listener) PRINT (conn) cursor = conn.cursor () sql = "SELECT * from T_msis_sm_role" #一次取一条数据, Row is the tuple data cursor.execute (SQL) while (1):    row = Cursor.fetchone ()    if row = = None: Break    print (row) print ( "--------------------------------------") #一次取所有数据, rows for the tuple list data cursor.execute (SQL) rows = Cursor.fetchall () for row in Rows:    print (ROW) #支持对数据库的插入, update, and delete operations. Input operation SQL, execute no return. def other_operation (SQL):    cursor.execute (SQL)    conn.commit ()    print (SQL) Cursor.close () Conn.close ()

  

Examples of encapsulation into class code:

#oracle操作类import cx_oracleclass oracle_class:    user = "Yzwxceshi"    passwd = "Yzwxceshi"    listener = ' 192.168.20.191:1521/ORCL '    conn = cx_oracle.connect (user, passwd, listener)    cursor = Conn.cursor ()    # Query operation: Fetch all data at once. Enter query SQL to return the result tuple list.    def querydata (self, SQL):        list_result = []        self.cursor.execute (sql)        rows = Self.cursor.fetchall ()        for row in rows:            list_result.append (Row)        return List_result    # supports INSERT, UPDATE, and delete operations on the database. Input operation SQL, no return.    def other_operation (self, SQL):        self.cursor.execute (SQL)        self.conn.commit ()        print (SQL)    # Close the connection, release the resource    def close_all (self):        self.cursor.close ()        self.conn.close ()

  

Calling code:

#用于测试数据库操作from oracle_class Import oracle_classselect_sql = "Select Role_name from t_msis_sm_role" insert_sql = "Insert in To T_msis_sm_role  values (' role_name ', ' normal user ') "update_sql =" Update t_msis_sm_role set role_desc = ' Test user ' where RO le_id = "Delete_sql =" Delete from t_msis_sm_role where  role_id = "Oracle_obj = Oracle_class () oracle_obj.other_op Eration (delete_sql) List = Oracle_obj.querydata (select_sql) print (list) Oracle_obj.close_all ()

  



Python Oracle operations (cx_oracle)

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.