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 2020-05-07 13:06  羽竹  阅读(1047)  评论(0编辑  收藏  举报