In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to use cx_oracle in the database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Connect
Import cx_Oracle
# use tnsnames file alias links
# ora = cx_Oracle.connect ('scott/tiger@orcl')
# use a string and pass in a parameter link
# ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
# using strings, passing in usernames, passwords, etc.
# ora = cx_Oracle.connect ('scott','tiger','192.168.56.152:1521/orcl')
# use dsn to parse into tns strings and connect to the database
# tnsname = cx_Oracle.makedsn ('192.168.56.152 (' 192.168.56.152)
# ora = cx_Oracle.connect ('scott','tiger',tnsname)
# use sysdba or other role links
Ora = cx_Oracle.connect ('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)
Cursor = ora.cursor ()
# use location corresponding parameters
Cursor.execute ('select * from scott.t1 where DEPTNO =: 1, (10,))
Print (cursor.fetchall ())
Cursor.close ()
Ora.close ()
Query
# fetchall
Import cx_Oracle
Ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
Cursor = ora.cursor ()
Cursor.execute ('select * from emp')
Print (cursor.fetchall ())
Cursor.close ()
Ora.close ()
# fetchone
Import cx_Oracle
Ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
Cursor = ora.cursor ()
Cursor.execute ('select * from emp')
While 1:
Res = cursor.fetchone ()
If res = = None:
Break
Print (res)
Cursor.close ()
Ora.close ()
# fetchmany
# use dsn to parse into tns strings and connect to the database
Tnsname = cx_Oracle.makedsn ('192.168.56.151)
Ora = cx_Oracle.connect ('system','oracle',tnsname)
Cursor = ora.cursor ()
Cursor.execute ('select * from dba_objects')
ResCount=0
While 1:
Res = cursor.fetchmany (10)
If res = = []:
Break
Print (res)
ResCount + = 10
Cursor.close ()
Ora.close ()
# using bind variables
Import cx_Oracle
Ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
Cursor = ora.cursor ()
# use location corresponding parameters
Cursor.execute ('select * from T1 where DEPTNO =: 1, (10,))
Print (cursor.fetchall ())
# pass in parameters using a dictionary
Param= {'dno':20}
Cursor.execute ('select * from T1 where DEPTNO =: dno',param)
Print (cursor.fetchall ())
Cursor.execute ('select * from T1 where DEPTNO =: dno or DNAME=:dn',dno=40,dn='ACCOUNTING')
Print (cursor.fetchall ())
Cursor.close ()
Ora.close ()
Add, delete, change data and execute multiple times
Import cx_Oracle
# use tnsnames file alias links
# ora = cx_Oracle.connect ('scott/tiger@orcl')
# use a string and pass in a parameter link
# ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
# using strings, passing in usernames, passwords, etc.
# ora = cx_Oracle.connect ('scott','tiger','192.168.56.152:1521/orcl')
# use dsn to parse into tns strings and connect to the database
Tnsname = cx_Oracle.makedsn ('192.168.56.152)
Ora = cx_Oracle.connect ('scott','tiger',tnsname)
# use sysdba or other role links
# ora = cx_Oracle.connect ('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)
Cursor = ora.cursor ()
Parameters used in cursor.execute ('insert into T1 values (50 DBA','CHINA' 1 jewel 2), (' DBA','CHINA')) # sql
Ora.commit ()
Cursor.execute ('select * from T1')
While 1:
Res = cursor.fetchone ()
If res = = None:
Break
Print (res)
Cursor.close ()
Ora.close ()
Import cx_Oracle
# use tnsnames file alias links
# ora = cx_Oracle.connect ('scott/tiger@orcl')
# use a string and pass in a parameter link
# ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
# using strings, passing in usernames, passwords, etc.
# ora = cx_Oracle.connect ('scott','tiger','192.168.56.152:1521/orcl')
# use dsn to parse into tns strings and connect to the database
Tnsname = cx_Oracle.makedsn ('192.168.56.152)
Ora = cx_Oracle.connect ('scott','tiger',tnsname)
# use sysdba or other role links
# ora = cx_Oracle.connect ('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)
Cursor = ora.cursor ()
Cursor.prepare ('update T1 set LOC=:loc where DEPTNO=:dno')
Cursor.execute (None, {'loc':'BEIJING','dno':50}) # uses the prepare function, which allows you to pass parameters directly without passing in a sql statement in execute. Note: the first parameter here must be None
Ora.commit ()
Cursor.execute ('select * from T1')
While 1:
Res = cursor.fetchone ()
If res = = None:
Break
Print (res)
Cursor.close ()
Ora.close ()
Import cx_Oracle
# use tnsnames file alias links
# ora = cx_Oracle.connect ('scott/tiger@orcl')
# use a string and pass in a parameter link
# ora = cx_Oracle.connect ('scott/tiger@192.168.56.152:1521/orcl')
# using strings, passing in usernames, passwords, etc.
# ora = cx_Oracle.connect ('scott','tiger','192.168.56.152:1521/orcl')
# use dsn to parse into tns strings and connect to the database
Tnsname = cx_Oracle.makedsn ('192.168.56.152)
Ora = cx_Oracle.connect ('scott','tiger',tnsname)
# use sysdba or other role links
# ora = cx_Oracle.connect ('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)
Cursor = ora.cursor ()
# execute multiple statements
List1 = [(60, (60)), ((70)), (70), (), (70), (()), ()]
Cursor.prepare ('insert into T1 values (: 1 recorder 2 jewel 3)')
Cursor.executemany (None,list1) # uses the prepare function, which allows you to pass parameters directly without passing in a sql statement in execute. Note: the first parameter here must be None
Ora.commit ()
Cursor.execute ('select * from T1')
While 1:
Res = cursor.fetchone ()
If res = = None:
Break
Print (res)
Cursor.close ()
Ora.close ()
Call functions and stored procedures
# calling stored procedures
Cursor.callproc (name, parameters= [], keywordParameters= {})
# call function
Cursor.callfunc (name, returnType, parameters= [], keywordParameters= {})
# cx_Oracle.STRING
There is a conversion relationship between object types of cx_Oracle and Python.
Oraclecx_OraclePythonVARCHAR2, NVARCHAR2, LONGcx_Oracle.STRINGstrCHARcx_Oracle.FIXED_CHARstrNUMBERcx_Oracle.NUMBERintFLOATcx_Oracle.NUMBERfloatDATEcx_Oracle.DATETIMEdatetime.datetimeTIMESTAMPcx_Oracle.TIMESTAMPdatetime.datetimeCLOBcx_Oracle.CLOBcx_Oracle.LOBBLOBcx_Oracle.BLOBcx_Oracle.LOB
Get Chinese garbled code
Import os
Os.environ ['NLS_LANG'] =' SIMPLIFIED CHINESE_CHINA.UTF8'
# or os.environ ['NLS_LANG'] =' AMERICAN_AMERICA.AL32UTF8'
Thank you for reading! This is the end of the article on "how to use cx_oracle in the database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.