千家信息网

数据库中cx_oracle怎么用使用

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章给大家分享的是有关数据库中cx_oracle怎么用使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。连接import cx_Oracle#使用tnsnames文件别
千家信息网最后更新 2026年10月31日数据库中cx_oracle怎么用使用

这篇文章给大家分享的是有关数据库中cx_oracle怎么用使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

连接

import cx_Oracle


#使用tnsnames文件别名链接

# ora = cx_Oracle.connect('scott/tiger@orcl')


#使用字符串,传入一个参数链接

# ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl')


# 使用字符串,分别传入用户名密码等

# ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl')


# 使用dsn解析成tns字符串,连接数据库

# tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl')

# ora = cx_Oracle.connect('scott','tiger',tnsname)


#使用sysdba或者其他角色链接

ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)



cursor = ora.cursor()


#使用位置对应参数

cursor.execute('select * from scott.t1 where DEPTNO = :1',(10,))

print(cursor.fetchall())



cursor.close()

ora.close()

查询

#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


# 使用dsn解析成tns字符串,连接数据库

tnsname = cx_Oracle.makedsn('192.168.56.151','1521','orcl')

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()


#使用绑定变量

import cx_Oracle


ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl')

cursor = ora.cursor()


#使用位置对应参数

cursor.execute('select * from t1 where DEPTNO = :1',(10,))

print(cursor.fetchall())


#使用字典传入参数

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()

增、删、改 数据和多次执行

import cx_Oracle


#使用tnsnames文件别名链接

# ora = cx_Oracle.connect('scott/tiger@orcl')


#使用字符串,传入一个参数链接

# ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl')


# 使用字符串,分别传入用户名密码等

# ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl')


# 使用dsn解析成tns字符串,连接数据库

tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl')

ora = cx_Oracle.connect('scott','tiger',tnsname)


#使用sysdba或者其他角色链接

# ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)

cursor = ora.cursor()


cursor.execute('insert into t1 values(50,:1,: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


#使用tnsnames文件别名链接

# ora = cx_Oracle.connect('scott/tiger@orcl')


#使用字符串,传入一个参数链接

# ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl')


# 使用字符串,分别传入用户名密码等

# ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl')


# 使用dsn解析成tns字符串,连接数据库

tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl')

ora = cx_Oracle.connect('scott','tiger',tnsname)


#使用sysdba或者其他角色链接

# 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}) #使用了prepare函数,在execute里面可以不传入sql语句,直接传入参数。注意:这里的第一个参数必须为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


#使用tnsnames文件别名链接

# ora = cx_Oracle.connect('scott/tiger@orcl')


#使用字符串,传入一个参数链接

# ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl')


# 使用字符串,分别传入用户名密码等

# ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl')


# 使用dsn解析成tns字符串,连接数据库

tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl')

ora = cx_Oracle.connect('scott','tiger',tnsname)


#使用sysdba或者其他角色链接

# ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA)

cursor = ora.cursor()


#执行多条语句

list1 = [(60,'Enginer','Sydney'),(70,'Diver','South Africa')]

cursor.prepare('insert into t1 values(:1,:2,:3)')

cursor.executemany(None,list1) #使用了prepare函数,在execute里面可以不传入sql语句,直接传入参数。注意:这里的第一个参数必须为None


ora.commit()


cursor.execute('select * from t1')

while 1:

res = cursor.fetchone()

if res == None:

break

print(res)



cursor.close()

ora.close()

调用函数和存储过程

#调用存储过程

cursor.callproc(name, parameters=[], keywordParameters={})

#调用函数

cursor.callfunc(name, returnType, parameters=[], keywordParameters={})

#cx_Oracle.STRING

cx_Oracle、Python的对象类型之间存在转换关系

Oraclecx_OraclePython
VARCHAR2, NVARCHAR2, LONGcx_Oracle.STRINGstr
CHARcx_Oracle.FIXED_CHARstr
NUMBERcx_Oracle.NUMBERint
FLOATcx_Oracle.NUMBERfloat
DATEcx_Oracle.DATETIMEdatetime.datetime
TIMESTAMPcx_Oracle.TIMESTAMPdatetime.datetime
CLOBcx_Oracle.CLOBcx_Oracle.LOB
BLOBcx_Oracle.BLOBcx_Oracle.LOB

获取中文乱码

import os

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

#或者os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'

感谢各位的阅读!关于"数据库中cx_oracle怎么用使用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

字符 字符串 参数 链接 数据 数据库 函数 别名 密码 文件 用户 用户名 角色 语句 位置 内容 更多 篇文章 过程 存储 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网金融科技 广东万易互联网科技有限公司惠州分公司 科技互联网公司装修 互联网巨头反垄断金融科技 互联网科技媒体 天津摩尔互联网科技 科技互联网公司logo 联恩商钥互联网科技有限公司 佩信高科技互联网 互联网科技公司的市场部门 湖北码尼互联网科技有限公司电话 晓花互联网科技有限公司网址 乌鲁木齐新华智原互联网科技学校 山东子淼互联网科技有限公司 智帮互联网科技公司最近动向 福州德业互联网科技 普陀区互联网教育科技管理办法 西安左小互联网科技有限公司 美国五大互联网科技公司市值多少 优雇互联网科技有限公司 广州大一互联网络科技有限公司 互联网金融公司科技 海南购全球互联网科技 科技与互联网哪个更好 移动互联网科技公司 互联网在线教育科技创业 中和天地互联网科技有限公司 山东有哪些互联网科技公司 广州梦谷互联网科技有限公司领导 北京军民互联网科技公司
0