返回列表 发帖

Win7使用Python3脚本远程管理Oracle11gR2

笺注:这是在 Navicat连接Oracle11gR2 的基础上进行的。

实验中,客户端为Win7(32位)

客户端Python的版本:
C:\Users\jacky\Desktop>python --version
Python 3.6.8

C:\Users\jacky\Desktop>


客户端下载第三方库(cx_Oracle):
https://www.lfd.uci.edu/~gohlke/pythonlibs/#cx_oracle%E2%80%8B
图片1.png
2022-7-8 10:26



客户端安装第三方库(cx_Oracle):
C:\Users\jacky\Desktop>pip3 install cx_Oracle-8.1.0-cp36-cp36m-win32.whl
Processing c:\users\jacky\desktop\cx_oracle-8.1.0-cp36-cp36m-win32.whl
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-8.1.0

C:\Users\jacky\Desktop>


列出当前环境所有已经安装的第三方库的名称和其版本号:
C:\Users\jacky\Desktop>pip3 freeze
cx-Oracle==8.1.0

C:\Users\jacky\Desktop>



客户端下载合适的Oracle Instant Client:
https://www.oracle.com/database/technologies/instant-client/downloads.html
图片2.png
2022-7-8 10:33


图片3.png
2022-7-8 10:33



解压文件instantclient-basic-nt-12.1.0.2.0.zip到C:\oracle里面:
图片4.png
2022-7-8 10:34


复制文件夹C:\oracle\instantclient_12_1里面所有的.dll文件到
C:\Users\jacky\AppData\Local\Programs\Python\Python36-32\Lib\site-packages里面:(根据Python的安装目录)
图片5.png
2022-7-8 10:34




######

例子一:
客户端远程测试与数据库连接的脚本:(C:\Users\jacky\Desktop\xx.py)

脚本内容:
#coding=utf-8
import cx_Oracle

def db_connect():

        #远程连接数据库;Oracle服务器的IP地址为192.168.168.163
        db = cx_Oracle.connect('happy/mima@192.168.168.163:1521/ORCL')
       
        cursor = db.cursor()
       
        Sql_1 = "SELECT version FROM product_component_version WHERE substr(product, 1, 6) = 'Oracle'" #查看数据库版本信息
        cursor.execute(Sql_1)
       
        Result_1 = cursor.fetchone()
        print(Result_1)
        print(type(Result_1))
       
        print("-" * 10)
        print(Result_1[0]) #输出元组的第一个元素
       
        print("-" * 10)
        print(f"数据库的版本信息:{Result_1[0]}")
       
        db.close() #关闭数据库连接

def func_main():
        try:
                db_connect()
        except Exception as e:
                print(f"数据库连接失败,原因: {e}")

if __name__ == "__main__":

        func_main()


脚本运行的结果:(连接成功时)
C:\Users\jacky\Desktop>python xx.py
('11.2.0.1.0',)
<class 'tuple'>
----------
11.2.0.1.0
----------
数据库的版本信息:11.2.0.1.0

C:\Users\jacky\Desktop>


假如不配置好Oracle Instant Client会出现如下错误提示:
C:\Users\jacky\Desktop>python xx.py
数据库连接失败,原因: DPI-1047: Cannot locate a 32-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

C:\Users\jacky\Desktop>



######

例子二:
客户端远程输出数据库最大连接数的脚本:

脚本内容:
#coding=utf-8
import cx_Oracle

def query_data():
        db = cx_Oracle.connect('happy/mima@192.168.168.163:1521/ORCL')
        cursor = db.cursor()
               
        Sql_1 = "select value from v$parameter where name='processes'" #输出数据库最大连接数
        cursor.execute(Sql_1)
       
        Result_1 = cursor.fetchone()
        print(Result_1[0])
       
        db.commit() #把执行任务提交到数据库
        db.close()

def func_main():
        try:
                query_data()
        except Exception as e:
                print(f"输出失败,原因: {e}")

if __name__ == "__main__":

        func_main()


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
150

C:\Users\jacky\Desktop>



######

例子三:
客户端远程输出数据库当前打开的连接数的脚本:

脚本内容:
#coding=utf-8
import cx_Oracle

def query_data():
        db = cx_Oracle.connect('happy/mima@192.168.168.163:1521/ORCL')
        cursor = db.cursor()
               
        Sql_1 = "select count(*) from v$session" #输出数据库当前打开的连接数
        cursor.execute(Sql_1)
       
        Result_1 = cursor.fetchone()
        print(Result_1[0])
       
        db.commit() #把执行任务提交到数据库
        db.close()

def func_main():
        try:
                query_data()
        except Exception as e:
                print(f"输出失败,原因: {e}")

if __name__ == "__main__":

        func_main()


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
23

C:\Users\jacky\Desktop>



######

例子四:
客户端远程输出数据库当前处于激活状态的连接数的脚本:

脚本内容:
#coding=utf-8
import cx_Oracle

def query_data():
        db = cx_Oracle.connect('happy/mima@192.168.168.163:1521/orcl')
        cursor = db.cursor()
               
        Sql_1 = "select count(*) from v$session where status='ACTIVE'" #查看数据库当前处于激活状态的连接数
        cursor.execute(Sql_1)
       
        Result_1 = cursor.fetchone()
        print(Result_1[0])
       
        db.commit() #把执行任务提交到数据库
        db.close()

def func_main():
        try:
                query_data()
        except Exception as e:
                print(f"输出失败,原因: {e}")

if __name__ == "__main__":

        func_main()


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
22

C:\Users\jacky\Desktop>





############
############

Win7客户端卸载第三方库(cx_Oracle):
C:\Users\jacky\Desktop>pip3 uninstall cx_Oracle -y
Uninstalling cx-Oracle-8.0.1:
  Successfully uninstalled cx-Oracle-8.0.1


C:\Users\jacky\Desktop>





相关文章:
Python3脚本管理Oracle11gR2

返回列表