标题:
Python3脚本管理Oracle11gR2
[打印本页]
作者:
admin
时间:
2020-10-29 08:38
标题:
Python3脚本管理Oracle11gR2
笺注:这是在
Navicat连接Oracle11gR2
的基础上进行的。 相关软件的下载链接:
https://pan.baidu.com/s/1Bip6wckcZ8eEiMoJIii4lA
提取码:mwoi Oracle Linux6.9安装Python3: 安装依赖软件包:(以本地光盘作为Yum源即可) [root@oracle ~]# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel wget gcc-c++ make 编译安装Python3: tar -zxvf Python-3.6.8.tgz mv Python-3.6.8 /usr/local/ cd /usr/local/Python-3.6.8/ ./configure make make install 给新Python3创建新链接: [root@oracle ~]# ln -s /usr/local/bin/python3.6 /usr/bin/python3 Python2和Python3实现共存了:
下载
(128.88 KB)
2022-7-7 08:55
Oracle服务器连接公网安装第三方库(cx_Oracle):(可以指定软件的版本) [root@oracle ~]# pip3 install
cx_Oracle==8.0.1
-i http://mirrors.aliyun.com/pypi/simple --trusted-host=
mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting cx_Oracle==8.0.1 Downloading
http://mirrors.aliyun.com/pypi/packages/9f/ce/9117cc84d05800b7962f1842bf5fb20455ee08264cda052a3c79d476cf01/cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
(756kB) 100% |████████████████████████████████| 757kB 4.3MB/s Installing collected packages: cx-Oracle Successfully installed cx-Oracle-8.0.1 [root@oracle ~]# 列出当前环境所有已经安装的第三方库的名称和其版本号: [root@oracle ~]# pip3 freeze
cx-Oracle==8.0.1
[root@oracle ~]# 由此还可以得获得第三方库(cx_Oracle)的下载地址:(需要另外手动下载软件包) [root@oracle ~]# wget
http://mirrors.aliyun.com/pypi/packages/9f/ce/9117cc84d05800b7962f1842bf5fb20455ee08264cda052a3c79d476cf01/cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
软件包下载成功: [root@oracle ~]# du -sh cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
740K cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
[root@oracle ~]# ###### 例子一: Oracle服务器本地测试与数据库连接的脚本:(/root/xx.py) [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def db_connect(): #本地连接数据库;使用数据库用户
happy
,数据库实例
ORCL
db=cx_Oracle.connect('happy/
mima
@127.0.0.1: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
() #使用
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() 脚本运行的结果: [root@oracle ~]# python3 xx.py 数据库连接失败,原因: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See
https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
for help [root@oracle ~]# 根据提示登录网站查看文档,下载软件包:
https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
安装相关RPM包(CentOS6/Redhat6要安装oracle-instantclient18.3-basic): [root@oracle ~]# rpm -ivh
oracle-instantclient18.3-basic-18.3.0.0.0-3.x86_64.rpm
warning: oracle-instantclient18.3-basic-18.3.0.0.0-3.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Preparing... ########################################### [100%] 1:oracle-instantclient18.########################################### [100%] [root@oracle ~]# [root@oracle ~]# sudo sh -c "echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf" [root@oracle ~]# [root@oracle ~]# cat /etc/ld.so.conf.d/oracle-instantclient.conf
/usr/lib/oracle/18.3/client64/lib
[root@oracle ~]# [root@oracle ~]# sudo ldconfig [root@oracle ~]# 脚本运行的结果:(连接成功时) [root@oracle ~]# python3 xx.py ('11.2.0.1.0',)
----------
11.2.0.1.0
---------- 数据库的版本信息:
11.2.0.1.0
[root@oracle ~]# 脚本运行的结果:(因用户密码错误,连接失败时) [root@oracle ~]# python3 xx.py 数据库连接失败,原因:
ORA-01017: invalid username/password; logon denied
[root@oracle ~]# ###### 数据库用户happy已有的表STU_01的表结构:
下载
(101.79 KB)
2022-7-7 09:03
例子二: Oracle服务器本地一次性插入多条记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle import datetime dt = datetime.datetime.now() tt_1 = dt.strftime('%Y-%m-%d %H:%M:%S') #当前时间 def insert_record(): global db db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "INSERT into STU_01 (ID,NAME,AGE,GRADE,ADDRESS,MATH,CHINESE,SHIJIAN)" \ " values (1,'小明',15,'三年二班','广东省,广州市',60.5,70.5,to_timestamp('2020-10-5 19:3:15','yyyy-mm-ddhh24:mi:ss'))" cursor.execute(Sql_1) Key_1 = 2 Key_2 = '李大杰' Key_3 = 16 Key_4 = '三年二班' Key_5 = '广东省,佛山市' Key_6 = 66 Key_7 = 78 Key_8 = "to_timestamp('2020-10-06 10:23:15','yyyy-mm-ddhh24:mi:ss')" Sql_2 = f"INSERT into STU_01 (ID,NAME,AGE,GRADE,ADDRESS,MATH,CHINESE,SHIJIAN) VALUES({Key_1},'{Key_2}',{Key_3},'{Key_4}','{Key_5}',{Key_6},{Key_7},{Key_8})" cursor.execute(Sql_2) Key_1 = 3 Key_2 = 'Zhuohua' Key_3 = 18 Key_4 = '三年十二班' Key_5 = '四川省,成都市' Key_6 = 100 Key_7 = 99 Key_8 = f"to_timestamp('{tt_1}','yyyy-mm-ddhh24:mi:ss')" Sql_3 = f"INSERT into STU_01 (ID,NAME,AGE,GRADE,ADDRESS,MATH,CHINESE,SHIJIAN) VALUES({Key_1},'{Key_2}',{Key_3},'{Key_4}','{Key_5}',{Key_6},{Key_7},{Key_8})" cursor.execute(Sql_3) db.commit() #把执行任务提交到数据库;必须是所有SQL语句都执行成功才会提交 db.close() def func_main(): try: insert_record() except Exception as e: print(f"插入记录失败,原因: {e}") db.rollback() #如果发生错误就回滚 else: print("插入记录成功。") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py
插入记录成功。
[root@oracle ~]# 插入记录成功:
下载
(90.9 KB)
2022-7-7 09:04
###### 例子三: Oracle服务器本地输出数据库中某个表的所有记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "select * from STU_01" #输出表STU_01中的所有记录 cursor.execute(Sql_1) Result_1 = cursor.
fetchall
() #使用
fetchall
可以获取多个元素 print(Result_1) print(type(Result_1)) print("-" * 10) for Key_1 in Result_1: #自定义输出格式 ID = Key_1[0] NAME = Key_1[1] AGE = Key_1[2] GRADE = Key_1[3]
.strip()
#去除左边和右边的空格、换行符 ADDRESS = Key_1[4] MATH = Key_1[5] CHINESE = Key_1[6] SHIJIAN = str(Key_1[7]) print(f"id={ID} && 姓名={NAME} && 年龄={AGE} && 班级={GRADE} && 地址={ADDRESS} && 数学成绩={MATH} && 语文成绩={CHINESE} && 时间={SHIJIAN}") db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py [(1, '小明', 15, '三年二班 ',
, 60.5, 70.5, datetime.datetime(2020, 10, 5, 19, 3, 15)), (2, '李大杰', 16, '三年二班 ',
, 66.0, 78.0, datetime.datetime(2020, 10, 6, 10, 23, 15)), (3, 'Zhuohua', 18, '三年十二班 ',
, 100.0, 99.0, datetime.datetime(2020, 10, 29, 8, 33))]
---------- id=1 && 姓名=小明 && 年龄=15 && 班级=三年二班 && 地址=广东省,广州市 && 数学成绩=60.5 && 语文成绩=70.5 && 时间=2020-10-05 19:03:15 id=2 && 姓名=李大杰 && 年龄=16 && 班级=三年二班 && 地址=广东省,佛山市 && 数学成绩=66.0 && 语文成绩=78.0 && 时间=2020-10-06 10:23:15 id=3 && 姓名=Zhuohua && 年龄=18 && 班级=三年十二班 && 地址=四川省,成都市 && 数学成绩=100.0 && 语文成绩=99.0 && 时间=2020-10-29 08:33:00 [root@oracle ~]# ###### 例子四: Oracle服务器本地输出数据库中某个表里符合指定条件的记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/orcl') cursor = db.cursor() Sql_1 = "select * from STU_01 where GRADE = '三年二班'" #SQL语句 cursor.execute(Sql_1) Result_1 = cursor.
fetchall()
for Key_1 in Result_1: #自定义输出格式 ID = Key_1[0] NAME = Key_1[1] AGE = Key_1[2] GRADE = Key_1[3].
rstrip()
#单单去除右边的空格、换行符 ADDRESS = Key_1[4] MATH = Key_1[5] CHINESE = Key_1[6] SHIJIAN = str(Key_1[7]) print(f"id={ID} && 姓名={NAME} && 年龄={AGE} && 班级={GRADE} && 地址={ADDRESS} && 数学成绩={MATH} && 语文成绩={CHINESE} && 时间={SHIJIAN}") db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py id=1 && 姓名=小明 && 年龄=15 && 班级=三年二班 && 地址=广东省,广州市 && 数学成绩=60.5 && 语文成绩=70.5 && 时间=2020-10-05 19:03:15 id=2 && 姓名=李大杰 && 年龄=16 && 班级=三年二班 && 地址=广东省,佛山市 && 数学成绩=66.0 && 语文成绩=78.0 && 时间=2020-10-06 10:23:15 [root@oracle ~]# ###### 例子五: Oracle服务器本地输出根据关键字搜索出来的记录的脚本:(结果只取一个值) [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/orcl') cursor = db.cursor() Sql_1 = "select * from STU_01 where NAME = '
Zhuohua
'" #关键字区分英文字母大小写 cursor.execute(Sql_1) Result_1 = cursor.
fetchall()
for Key_1 in Result_1: #自定义输出格式 ID = Key_1[0] NAME = Key_1[1] AGE = Key_1[2] GRADE = Key_1[3].
rstrip()
#单单去除右边的空格、换行符 ADDRESS = Key_1[4] MATH = Key_1[5] CHINESE = Key_1[6] SHIJIAN = str(Key_1[7]) print(f"id={ID} && 姓名={NAME} && 年龄={AGE} && 班级={GRADE} && 地址={ADDRESS} && 数学成绩={MATH} && 语文成绩={CHINESE} && 时间={SHIJIAN}") print("-" * 10) print(f"{NAME}的年龄:") print(AGE) print(type(AGE)) db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py id=3 && 姓名=Zhuohua && 年龄=18 && 班级=三年十二班 && 地址=四川省,成都市 && 数学成绩=100.0 && 语文成绩=99.0 && 时间=2020-10-29 08:33:00 ---------- Zhuohua的年龄:
18
[root@oracle ~]# ###### 例子六: Oracle服务器本地输出根据关键字搜索出来的记录的脚本:(结果只取一个值) [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "select * from STU_01 where NAME = 'Zhuohua'" cursor.execute(Sql_1) Result_1 = cursor.
fetchall()
for Key_1 in Result_1: #自定义输出格式 ID = Key_1[0] NAME = Key_1[1] AGE = Key_1[2] GRADE = Key_1[3].
rstrip()
#单单去除右边的空格、换行符 ADDRESS = Key_1[4] MATH = Key_1[5] CHINESE = Key_1[6] SHIJIAN = str(Key_1[7]) print(f"id={ID} && 姓名={NAME} && 年龄={AGE} && 班级={GRADE} && 地址={ADDRESS} && 数学成绩={MATH} && 语文成绩={CHINESE} && 时间={SHIJIAN}") print("-" * 10) print(f"{Result_1[0][1]}的语文成绩:") print(Result_1[0][6]) print(type(Result_1[0][6])) db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py id=3 && 姓名=Zhuohua && 年龄=18 && 班级=三年十二班 && 地址=四川省,成都市 && 数学成绩=100.0 && 语文成绩=99.0 && 时间=2020-10-29 08:33:00 ---------- Zhuohua的语文成绩:
99.0
[root@oracle ~]# ###### 例子七: Oracle服务器本地输出根据关键字搜索出来的记录的脚本:(结果只取一个值) [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "select * from STU_01 where NAME = 'Zhuohua'" cursor.execute(Sql_1) Result_1 = cursor.
fetchall()
Result_2 = Result_1[
0
] #列表的第一个元素,即第一条记录 NAME = Result_2[1] CHINESE = Result_2[6] SHIJIAN = str(Result_2[7]) date = str(Result_2[7])
[0:10]
#输出前面10个字符 time = str(Result_2[7])
[-8:]
#输出最后8个字符 print(f"姓名={NAME} && 语文成绩={CHINESE} && 具体时间={SHIJIAN} && 日期部分={date} && 时间部分={time}") print("-" * 10) print(f"{Result_1[0][1]}的具体时间:") print(SHIJIAN) print(type(SHIJIAN)) db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py 姓名=Zhuohua && 语文成绩=99.0 && 具体时间=2020-10-29 08:33:00 && 日期部分=2020-10-29 && 时间部分=08:33:00 ---------- Zhuohua的具体时间:
2020-10-29 08:33:00
[root@oracle ~]# ###### 例子八: Oracle服务器本地更改记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def update_record(): global db db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "UPDATE STU_01 SET MATH = 88 WHERE name = '
Zhuohua
'" #关键字区分英文字母大小写 cursor.execute(Sql_1) Sql_2 = "UPDATE STU_01 SET ADDRESS = '广东省,江门市' WHERE Name like '__杰%'" cursor.execute(Sql_2) db.commit() #把执行任务提交到数据库;必须是所有SQL语句都执行成功才会提交 db.close() def func_main(): try: update_record() except Exception as e: print(f"更改记录失败,原因: {e}") db.rollback() #如果发生错误就回滚 else: print("更改记录成功。") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py
更改记录成功。
[root@oracle ~]# ###### 例子九: Oracle服务器本地更改记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle import datetime dt = datetime.datetime.now() tt_1 = dt.strftime('%Y-%m-%d %H:%M:%S') #当前时间 def update_record(): global db db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "UPDATE STU_01 SET SHIJIAN = to_timestamp('2020-10-07 21:24:5','yyyy-mm-ddhh24:mi:ss') WHERE NAME = '李大杰'" cursor.execute(Sql_1) Key_1 = f"to_timestamp('{tt_1}','yyyy-mm-ddhh24:mi:ss')" Sql_2 = f"UPDATE STU_01 SET SHIJIAN = {Key_1} WHERE ID = 3" cursor.execute(Sql_2) db.commit() #把执行任务提交到数据库;必须是所有SQL语句都执行成功才会提交 db.close() def func_main(): try: update_record() except Exception as e: print(f"更改记录失败,原因: {e}") db.rollback() #如果发生错误就回滚 else: print("更改记录成功。") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py
更改记录成功。
[root@oracle ~]# ###### 例子十: Oracle服务器本地删除记录的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def delete_record(): global db db=cx_Oracle.connect('happy/mima@127.0.0.1:1521/ORCL') cursor = db.cursor() Sql_1 = "delete from STU_01 WHERE NAME = '小明'" cursor.execute(Sql_1) db.commit() #把执行任务提交到数据库;必须是所有SQL语句都执行成功才会提交 db.close() def func_main(): try: delete_record() except Exception as e: print(f"删除记录失败,原因: {e}") db.rollback() #如果发生错误就回滚 else: print("删除记录成功。") if __name__ == "__main__": func_main() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py
删除记录成功。
[root@oracle ~]# ###### 例子十一: Oracle服务器本地输出数据库最大连接数的脚本: [root@oracle ~]# cat xx.py #coding=utf-8 import cx_Oracle def query_data(): db=cx_Oracle.connect('happy/mima@127.0.0.1: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() [root@oracle ~]# 脚本运行的结果: [root@oracle ~]# python3 xx.py
150
[root@oracle ~]# ############ ############ 服务器本地登录Oracle: [root@oracle ~]#
su - oracle
[oracle@oracle ~]$
sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 29 09:55:10 2020 Copyright (c) 1982, 2009, Oracle. All rights reserved. SQL>
conn /as sysdba;
Connected. SQL> 查看当前数据库的最大连接数: SQL> select value from v$parameter where name='processes'; VALUE --------------------------------------------------------------------------------
150
SQL> 修改数据库的最大连接数: SQL>
Alter system set processes=300 scope=spfile;
Alter system set processes=300 scope=spfile * ERROR at line 1:
ORA-32001: write to SPFILE requested but no SPFILE is in use
SQL> 解决办法: SQL>
create spfile from pfile;
File created. SQL> SQL>
shutdown immediate;
Database closed. Database dismounted. ORACLE instance shut down. SQL> SQL>
startup;
ORACLE instance started. Total System Global Area 910266368 bytes Fixed Size 2218672 bytes Variable Size 247465296 bytes Database Buffers 654311424 bytes Redo Buffers 6270976 bytes Database mounted. Database opened. SQL> 修改数据库的最大连接数为300: SQL> Alter system set processes=
300
scope=spfile; System altered. SQL> SQL>
shutdown immediate;
Database closed. Database dismounted. ORACLE instance shut down. SQL> SQL>
startup;
ORACLE instance started. Total System Global Area 910266368 bytes Fixed Size 2218672 bytes Variable Size 247465296 bytes Database Buffers 654311424 bytes Redo Buffers 6270976 bytes Database mounted. Database opened. SQL> 再次查看当前数据库的最大连接数:(服务器重启后,依然生效) SQL> select value from v$parameter where name='processes'; VALUE --------------------------------------------------------------------------------
300
SQL> ############ ############ Oracle服务器卸载第三方库(cx_Oracle): [root@oracle ~]# pip3 uninstall
cx_Oracle
-y
Uninstalling cx-Oracle-8.0.1: Successfully uninstalled cx-Oracle-8.0.1
[root@oracle ~]# Oracle服务器卸载RPM包oracle-instantclient18.3-basic: [root@oracle ~]# rpm -e
oracle-instantclient18.3-basic
[root@oracle ~]#
笺注:假如只是在远程客户端通过Python3脚本管理Oracle数据库,那么Oracle服务器连Python3都不需要安装。
############ ############ 客户端CentOS7.8离线安装第三方库(cx_Oracle): [root@ser1 ~]# ls *.whl
cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
[root@ser1 ~]# [root@ser1 ~]# pip3 install
cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl
Processing ./cx_Oracle-8.0.1-cp36-cp36m-manylinux1_x86_64.whl Installing collected packages: cx-Oracle Successfully installed cx-Oracle-8.0.1 [root@ser1 ~]# 客户端安装相关RPM包:(CentOS7/Redhat7要安装oracle-instantclient19.9-basic): [root@ser1 ~]# rpm -Uvh
oracle-instantclient19.9-basic-19.9.0.0.0-1.x86_64.rpm
准备中... ################################# [100%] 正在升级/安装... 1:oracle-instantclient19.9-basic-19################################# [100%] [root@ser1 ~]# [root@ser1 ~]# sudo sh -c "echo /usr/lib/oracle/19.9/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf" [root@ser1 ~]# [root@ser1 ~]# cat /etc/ld.so.conf.d/oracle-instantclient.conf
/usr/lib/oracle/19.9/client64/lib
[root@ser1 ~]# [root@ser1 ~]# sudo ldconfig [root@ser1 ~]# ###### 例子十二: 客户端远程测试与数据库连接的脚本:(/root/yy.py) [root@ser1 ~]# cat yy.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() 脚本运行的结果:(连接成功时) [root@ser1 ~]# python3 yy.py ('11.2.0.1.0',)
---------- 11.2.0.1.0 ---------- 数据库的版本信息:11.2.0.1.0 [root@ser1 ~]# ###### 例子十三: 客户端远程输出数据库最大连接数的脚本: [root@ser1 ~]# cat yy.py #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() 脚本运行的结果: [root@ser1 ~]# python3 yy.py
300
[root@ser1 ~]# ###### 例子十四: 客户端远程输出数据库中某个表的所有记录的脚本: [root@ser1 ~]# cat yy.py #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 * from STU_01" #输出表STU_01中的所有记录 cursor.execute(Sql_1) Result_1 = cursor.
fetchall()
for Key_1 in Result_1: #自定义输出格式 ID = Key_1[0] NAME = Key_1[1] AGE = Key_1[2] GRADE = Key_1[3].
strip()
#去除左边和右边的空格、换行符 ADDRESS = Key_1[4] MATH = Key_1[5] CHINESE = Key_1[6] SHIJIAN = str(Key_1[7]) print(f"id={ID} && 姓名={NAME} && 年龄={AGE} && 班级={GRADE} && 地址={ADDRESS} && 数学成绩={MATH} && 语文成绩={CHINESE} && 时间={SHIJIAN}") db.commit() #把执行任务提交到数据库 db.close() def func_main(): try: query_data() except Exception as e: print(f"输出失败,原因: {e}") if __name__ == "__main__": func_main() 脚本运行的结果: [root@ser1 ~]# python3 yy.py id=2 && 姓名=李大杰 && 年龄=16 && 班级=三年二班 && 地址=广东省,江门市 && 数学成绩=66.0 && 语文成绩=78.0 && 时间=2020-10-07 21:24:05 id=3 && 姓名=Zhuohua && 年龄=18 && 班级=三年十二班 && 地址=四川省,成都市 && 数学成绩=88.0 && 语文成绩=99.0 && 时间=2020-10-29 11:27:15 [root@ser1 ~]# 相关文章:
Oracle的SQL语句
Win7使用Python3脚本远程管理Oracle11gR2
CentOS8使用Python3脚本远程管理Oracle11gR2
CentOS8_在Docker中安装Oracle11gR2
CentOS8_在Docker中安装Oracle19c
Zabbix调用Python3脚本监控Linux下的Oracle(一)
CentOS6安装Python3
CentOS7安装Python3
Python3脚本管理Linux下的MySQL
图片附件:
图片1.png
(2022-7-7 08:55, 128.88 KB) / 下载次数 93
http://blog.zhuohua.store/attachment.php?aid=20068&k=95559353ca8392713e42d13920c2790b&t=1714158536&sid=a5sxsx
图片附件:
图片2.png
(2022-7-7 09:03, 101.79 KB) / 下载次数 80
http://blog.zhuohua.store/attachment.php?aid=20069&k=d21ab0983dbf89a8020641643b1c5aab&t=1714158536&sid=a5sxsx
图片附件:
图片3.png
(2022-7-7 09:04, 90.9 KB) / 下载次数 78
http://blog.zhuohua.store/attachment.php?aid=20070&k=50afca647f790be5bcfc37879f1c30e4&t=1714158536&sid=a5sxsx
欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/)
Powered by Discuz! 7.2