Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use cx_oracle in database

Shulou Source: shulou.com Published: 2022-05-31 13:59:28 09月14日 Update

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!

Tags: Characters strings parameters links data databases functions aliases passwords files users user names roles statements location content more articles procedures storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Shulou Tech Info MySQL Shulou Technology