从报错开始梳理用Python连接Oracle数据库
【操作步骤】
1.搭建环境:
win7-64、python-3.7.3、Oracle客户端:instantclient
2.下载库包:
python -m pip install cx_Oracle
3.导入模块:
import cx_Oracle
4.建立连接:
db = cx_Oracle.connect(“username”,“password”,“hostname/service_name”)
5.创建游标:
cursor = db.cursor()
6.执行事务:
#执行SQL语句
cursor.execute(sql)
#获取一条结果
cursor.fetchone()
#获取全部结果
cursor.fetchall()
7.关闭资源
db.close()
import cx_Oracle
conn = cx_Oracle.connect("root","rootroot","192.168.110.110/orcl")
cursor = conn.cursor()
sql = "select * from T1"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
pass
conn.close()
以上步骤是标准步骤,但是需要一些小的处理技巧。如果你导入库之后直接建立连接,会报如下错误:
DatabaseError: DPI-1047