一、安装cx_Oracle
二、下载oracle客户端
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
三、连接数据库
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir="D:\\instantclient_12_2")
conn = cx_Oracle.connect('用户名', '密码', 'host:port/SERVICE_NAME')
cusor = conn.cursor()
cursor = conn.cursor()
cursor.execute("select * from table")
results = cursor.fetchmany(2)#输出打印两条结果
所遇到的问题:
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: “The specified module could not be found”. See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
解决办法:
程序中指定client目录
cx_Oracle.init_oracle_client(lib_dir=“D:\instantclient_12_2”)
四、指定结果输出
方法一:遍历返回数据后,指定打印所需字段
try:
for row in results:
MARKET_ID = row[0]
EFFECTIVE_TIME = row[11]
EXPIRE_TIME = row[12]
STATUS = row[13]
EXT_MARKET_CODE = row[30]
# 打印结果
print("MARKET_ID=%s,EFFECTIVE_TIME=%s,EXPIRE_TIME=%s,STATUS=%s,EXT_MARKET_CODE=%s" % \
(MARKET_ID, EFFECTIVE_TIME, EXPIRE_TIME, STATUS, EXT_MARKET_CODE))
except:
print ("输出错误信息")