python 连接 oracle

  • pip install cx_Oracle
  • 下载oracel 客户端 instantclient-basic-windows.x64-18.5.0.0.0dbru.zip ,不下载客户端可能有以下报错:

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help

  • 解压文件到脚本 的下级目录,或者设置路径到环境变量
#encoding=utf-8
import cx_Oracle as oracle
import os
#print(os.environ["path"])
set_path = os.environ["path"]
oracle_path = "..\\instantclient_18_5"
os.environ["path"] = ';'.join((set_path,oracle_path))  # oracle 路径 暂时加到 环境变量 
#print(os.environ["path"])

def checkCodeFor2():
    print("##########查询2号验证码#############\n")
    db = oracle.connect("账号", "密码", 'ip:1521/库名')
    cur = db.cursor()
    while(1):
        emp_id = input("请输入查询的ID:\n")
        if emp_id:
            try:
                cur.execute('select a.code,a.id from db_test a where a.id={}'.format(emp_id))
                #cursor.execute('select 1;')
                data = cur.fetchone()
                print(data)
                check_sign = input("是否继续查询:Y/y 是 /N 否,其他任意字符都退出\n")
                if check_sign.lower() == "y":
                    pass
                else:
                    return 0
            except oracle.DatabaseError as e:
                msg_error = e
                print("可能数据输入错误,请检查数据正确性,错误:\n{}".format(msg_error))

        else:
            pass
if __name__=="__main__":
    checkCodeFor2()

posted on 2019-06-06 13:15  游荡的鱼  阅读(627)  评论(0编辑  收藏  举报

导航