python连接oracle数据库

连接方式

import cx_Oracle as cx
#conn=cx_Oracle.connect(‘用户名/密码@主机ip地址:端口号/Service Name(SID)') 
# 第一种 con = cx.connect('root', 'root123', '127.0.0.1:1521/orcl') # 第二种 con = cx.connect('root/root123@127.0.0.1:1521/orcl') # 第三种 dsn = cx.makedsn('127.0.0.1', '1521', 'orcl') connection = cx.connect('root', 'root123', dsn)

简单使用

import cx_Oracle as cx      #导入模块
con = cx.connect('root', 'root123', '127.0.0.1:1521/ORCL')  #创建连接
cursor = con.cursor()       #创建游标
cursor.execute("select * from TDER where ID='28'")  #执行sql语句
data = cursor.fetchone()        #获取一条数据
print(data)     #打印数据
cursor.close()  #关闭游标
con.close()     #关闭数据库连接

oracle数据库的操作和mysql的pymysql操作基本相同

复制代码
import cx_Oracle
import os

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
# conn = cx_Oracle.connect('expenses','expenses','10.10.110.12:1521/orcl')
# conn = cx_Oracle.connect('expenses/expenses@10.10.110.12:1521/orcl')
dsn = cx_Oracle.makedsn('10.10.110.12', '1521', 'orcl')
conn = cx_Oracle.connect('expenses', 'expenses', dsn)
cur = conn.cursor()
sql = 'select * from declarationmatter'
cur.execute(sql)
result = cur.fetchall()
print(result)
cur.close()
conn.close()
复制代码

 

posted on   羽竹  阅读(1053)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示