Oracle Database Operations in Python practice

Source: Internet
Author: User

1. To enable Python to operate the Oracle database, first install the cx_Oracle package. You can obtain the installation package through the following address:

[Plain]
  1. Http://cx-oracle.sourceforge.net/
2. You also need some class libraries of oracle. You need to install the Oracle Instant Client package on the machine running python.
[Plain]
  1. Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
Find the package that meets your platform and install it. Here I use the rpm package, so run the following command to install
[Plain]
  1. $ Sudo rpm-ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.i386.rpm
After installation, you need to set the environment variables as follows [plain]
  1. $ Export LD_LIBRARY_PATH =$ {LD_LIBRARY_PATH}:/usr/lib/oracle/11.2/client/lib
3. Create a simple python file and test whether the installation is successful. [python]
  1. ImportCx_Oracle
  2. Conn = cx_Oracle.connect ('Fkong/fkong@172.17.23.129/orcl')
  3. Cursor = conn. cursor ()
  4. Cursor.exe cute ("Select * from dual")
  5. Row = cursor. fetchone ()
  6. PrintRow [0]
  7. Cursor. close ()
  8. Conn. close ()
4. The following describes a database table creation and insertion Operation [python].
  1. ImportCx_Oracle
  2. Conn = cx_Oracle.connect ('Fkong/fkong@172.17.23.129/orcl')
  3. Cursor = conn. cursor ()
  4. Cursor.exe cute ("Create table test (id int, COL1 VARCHAR (32), COL2 VARCHAR (32), COL3 VARCHAR (32 ))")
  5. Cursor.exe cute ("Insert into test (ID, COL1, COL2, COL3) VALUES (1, 'A', 'B', 'C ')")
  6. Cursor.exe cute ("Insert into test (ID, COL1, COL2, COL3) VALUES (2, 'A', 'bb ', 'cc ')")
  7. Cursor.exe cute ("Insert into test (ID, COL1, COL2, COL3) VALUES (3, 'aaa', 'bbb ', 'ccc ')")
  8. Conn. commit ()
  9. Cursor. close ()
  10. Conn. close ()
5. next let's look at the query. there are usually two ways to query: one is to use cursor. fetchall () is used to obtain all query results and then iterate over one row. fetchone () gets a record until the obtained result is null. Let's look at the example below: [python]
  1. ImportCx_Oracle
  2. Conn = cx_Oracle.connect ('Fkong/fkong@172.17.23.129/orcl')
  3. Cursor = conn. cursor ()
  4. Cursor.exe cute ("SELECT * from test")
  5. Rows = cursor. fetchall ()
  6. ForRowInRows:
  7. Print "% D, % s"% (Row [0], Row [1], Row [2], Row [3])
  8. Print "Number of rows returned: % d"% Cursor. rowcount
  9. Cursor.exe cute ("SELECT * from test")
  10. While(1):
  11. Row = cursor. fetchone ()
  12. IfRow =None:
  13. Break
  14. Print "% D, % s"% (Row [0], Row [1], Row [2], Row [3])
  15. Print "Number of rows returned: % d"% Cursor. rowcount
  16. Cursor. close ()
  17. Conn. close ()

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.