Oracle database operations for Python combat

Source: Internet
Author: User

1. To enable Python to operate the Oracle database, you first need to install the Cx_oracle package, which can be obtained from the address below

[Plain]View PlainCopy
    1. http://cx-oracle.sourceforge.net/

2. Additional Oracle class libraries are required to install the Oracle Instant client package on a machine running Python, which can be obtained from the following address

[Plain]View PlainCopy
    1. Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

Find the package that fits your platform, then install it, I'm using the RPM package, so install it using the following command

[Plain]View PlainCopy
    1. $ sudo rpm-ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.i386.rpm

After the installation, you need to set the environment variables, as follows

[Plain]View PlainCopy
    1. $ Export Ld_library_path=${ld_library_path}:/usr/lib/oracle/11.2/client/lib

3. Create a simple Python file to test whether the installation was successful

[Python]View PlainCopy
    1. Import Cx_oracle
    2. conn = Cx_oracle.connect (' fkong/[email protected]/orcl ')
    3. cursor = Conn.cursor ()
    4. Cursor.execute ("SELECT * from dual")
    5. row = Cursor.fetchone ()
    6. Print row[0]
    7. Cursor.close ()
    8. Conn.close ()

4. Look at a Database build table and insert operation

[Python]View PlainCopy
  1. Import Cx_oracle
  2. conn = Cx_oracle.connect (' fkong/[email protected]/orcl ')
  3. cursor = Conn.cursor ()
  4. Cursor.execute ("CREATE TABLE TEST" (ID INT, COL1 varchar (+), COL2 varchar (), COL3 varchar (+)) ")
  5. Cursor.execute ("INSERT into TEST (ID, COL1, COL2, COL3) VALUES (1, ' A ', ' B ', ' C ')")
  6. Cursor.execute ("INSERT into TEST (ID, COL1, COL2, COL3) VALUES (2, ' AA ', ' BB ', ' cc ')")
  7. Cursor.execute ("INSERT into TEST (ID, COL1, COL2, COL3) VALUES (3, ' AAA ', ' BBB ', ' CCC ')")
  8. Conn.commit ()
  9. Cursor.close ()
  10. Conn.close ()

5. Here's a look at the query, there are usually two ways of querying: one is to get all the query results using Cursor.fetchall () and then one line of iterations, and the other one to get a record by Cursor.fetchone () until the result is empty. Take a look at the following example:

[Python]View PlainCopy
    1. Import Cx_oracle
    2. conn = Cx_oracle.connect (' fkong/[email protected]/orcl ')
    3. cursor = Conn.cursor ()
    4. Cursor.execute ("select * from TEST")
    5. rows = Cursor.fetchall ()
    6. For row in rows:
    7. print "%d,%s,%s,%s"% (row[0], row[1], row[2], row[3])
    8. Print "Number of rows returned:%d"% cursor.rowcount
    9. Cursor.execute ("select * from TEST")
    10. while (1):
    11. row = Cursor.fetchone ()
    12. if row = = None:
    13. Break
    14. print "%d,%s,%s,%s"% (row[0], row[1], row[2], row[3])
    15. Print "Number of rows returned:%d"% cursor.rowcount
    16. Cursor.close ()
    17. Conn.close ()

Oracle database operations for Python combat

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.