Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cx_Oracle to access Oracle in Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to use cx_Oracle to access Oracle in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

If you want to access and manipulate the Oracle database in Python, you can use cx_Oracle to do so.

Preparatory work

Python is installed, and this example uses Python version 2.7.

Install the local Oracle client and configure the database connection, using Oracle 11g in this example.

Download and install cx_Oracle

Download address: http://sourceforge.net/projects/cx-oracle/

Pay attention to the Oracle database version and local Python version when downloading. After downloading, install it directly.

Simple query operation

Import cx_Oracle# create database connection conn = cx_Oracle.connect ('dbuser/password@servername/sid') # get operation cursor cursor = conn.cursor () # execute query SQL, return value 1cursor.execute ("select 1 from dual") # get return result A record rs = cursor.fetchone () # execution completed, print prompt message print' result =% s'%rsresult = close connection, release resource cursor.close () conn.close ()

Execute DDL table-building statement

Cursor.execute ("" create table tmp_user (id number, name varchar2 (20)) "") print'completedcustomers'

Perform DML insert record

# insert a record cursor.execute ("insert into tmp_user values") "") # insert a record param = {'id':2,' name':'system'} cursor.execute ('insert into tmp_user values (: id,: name)', param) # insert multiple data at a time Parameter is in the form of dictionary list param= [{'id':3,'name':'John'}, {' id':4,'name':'Mary'}, {'id':5,'name':'Steven'}] cursor.executemany (' insert into tmp_user values (: id,:name)', param) # insert multiple data again The parameter is in the form of tuple list param= [] for i in range (6je 8): # [6je 7] param.append (I) cursor.executemany ('insert into tmp_user values (: 1 dint param.append 2)', param) # commit transaction conn.commit

Query the case of returning multiple records

Cursor.execute ("" select * from tmp_user "") # get 2 records R2 = cursor.fetchmany (2) print R2 [0], R2 [1] # get all remaining records rn = cursor.fetchall () for row in rn: print row# conditional query cursor.prepare ("select * from tmp_user where id > =: id") cursor.execute (None, {'id':7}) # None pay attention to case for row in cursor: print row

Execute the call stored procedure (see the next section)

After reading the above, do you have any further understanding of how to use cx_Oracle to access Oracle in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report