Python链接oracle数据库 例子一

 

 

 

 

 

 

 

step1:下载cx_Oracle模块,cmd--pip install cx_Oraclehtml

step2:数据库

复制代码
1 import cx_Oracle #引用模块cx_Oracle 2 conn=cx_Oracle.connect('truck/******@10.74.**.**:****/****') #链接数据库 3 c=conn.cursor() #获取cursor 4 x=c.execute('select sysdate from dual') #使用cursor进行各类操做 5 x.fetchone() 6 c.close() #关闭cursor 7 conn.close() #关闭链接
复制代码

报错:cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "The specified module could not be found".oracle

缘由:本机装的Python、cx_Oracle都是64位的,Navicat链接的Oracle instantclient版本为32位的,因此链接报错。fetch

解决方案:下载64位 instantclient---http://jvniu.jb51.net:81/201708/tools/instantclientx64_jb51.rar   或者  http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.htmlspa

操做:下载结束后,解压至  Navicat根目录,添加环境变量,重启链接脚本。.net

报错:listener does not currently know of service requested in connect descriptor3d

缘由:参数理解错误code

#conn=cx_Oracle.connect(‘用户名/密码@主机ip地址:端口号/Service Name(SID)') conn=cx_Oracle.connect('truck/******@10.74.**.**:****/****') 

正确输入参数以后,数据库链接成功htm

 Python链接Oracle若是有中文,可能会出乱码,可经过如下方法解决blog

1 import os 2 os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

同时,在Python脚本中,添加一行代码

# -*- coding: utf-8 -*-
相关文章
相关标签/搜索