基于python連接oracle導并出數(shù)據(jù)文件
python連接oracle,感覺table_list文件內(nèi)的表名,來卸載數(shù)據(jù)文件
主腳本:
import os import logging import sys import configparser import subprocess import cx_Oracle #判斷輸入?yún)?shù)個數(shù) class param(): def check_para(self): if len(sys.argv) != 1: print("請輸入正確的參數(shù):yyyymmdd") exit(1) else: print("繼續(xù)執(zhí)行") #根據(jù)配置文件獲取登錄信息 class get_dbini(): def get_db(self): config=configparser.ConfigParser() filepath="db.ini" if os.path.exists(filepath): config.read_file(open(filepath)) dbinfo=[config.get("db_oracle","username"),\ config.get("db_oracle","password"),\ config.get("db_oracle","ip"),\ config.get("db_oracle","dbsid")] else: loginfo.info("沒有那個配置文件") sys.exit(4) #聲明使用全局變量 global username,password,ip,dbsid username=dbinfo[0] password=dbinfo[1] ip=dbinfo[2] dbsid=dbinfo[3] loginfo.info(username+password+ip+dbsid) #導出表數(shù)據(jù) class exp_date(): def exp_table(self): with open('table_list','r') as f: list = f.readlines() for i in list: tablename = i.rstrip('\n') exportquery='sqluldr2 user='+username+'/'+password+'@'+ip+':1521/'+dbsid+' query="select * from '+tablename+';" head=no file='+tablename+'.dat field=0x03 record=0x030x0a safe=yes' loginfo.info("開始導出數(shù)據(jù): exportquery= "+exportquery) flag= subprocess.check_call(exportquery,shell=True) loginfo.info(flag) #打印日志 class log_set(): def logger_set(self): logger=logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) fh=logging.FileHandler('a.log','w') fh.setLevel(logging.INFO) ch=logging.StreamHandler() ch.setLevel(logging.ERROR) formatter = logging.Formatter('%(asctime)s -%(name)s -%(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) return logger if __name__=='__main__': loginfo=log_set().logger_set() param().check_para() get_dbini().get_db() exp_date().exp_table()
DB配置文件內(nèi)容:
db.ini
[db_oracle]
username=c##scott
password=tiger
ip=192.168.1.250
dbsid=orcl
表名字的配置文件:
table_list
BONUS
DEPT
EMP
LEAD_TABLE
SALGRADE
T1
TB_USER
TEST
XGJ
XGJ_2
運行結(jié)果:
[oracle@master2 tmp]$ python3 c.py
繼續(xù)執(zhí)行
0 rows exported at 2019-01-22 17:51:51, size 0 MB.
output file BONUS.dat closed at 0 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
4 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file DEPT.dat closed at 4 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
12 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file EMP.dat closed at 12 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
10 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file LEAD_TABLE.dat closed at 10 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
5 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file SALGRADE.dat closed at 5 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
5 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file T1.dat closed at 5 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
10 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file TB_USER.dat closed at 10 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
8 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file TEST.dat closed at 8 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
9 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file XGJ.dat closed at 9 rows, size 0 MB.
0 rows exported at 2019-01-22 17:51:52, size 0 MB.
8 rows exported at 2019-01-22 17:51:52, size 0 MB.
output file XGJ_2.dat closed at 8 rows, size 0 MB.
查看日志:
[oracle@master2 tmp]$ more a.log
2019-01-22 17:51:51,858 -mylogger -INFO - c##scotttiger192.168.1.250orcl
2019-01-22 17:51:51,858 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from BONUS;" head=no file=BON
US.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:51,949 -mylogger -INFO - 0
2019-01-22 17:51:51,949 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from DEPT;" head=no file=DEPT
.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,038 -mylogger -INFO - 0
2019-01-22 17:51:52,038 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from EMP;" head=no file=EMP.d
at field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,129 -mylogger -INFO - 0
2019-01-22 17:51:52,129 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from LEAD_TABLE;" head=no fil
e=LEAD_TABLE.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,299 -mylogger -INFO - 0
2019-01-22 17:51:52,300 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from SALGRADE;" head=no file=
SALGRADE.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,401 -mylogger -INFO - 0
2019-01-22 17:51:52,402 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from T1;" head=no file=T1.dat
field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,490 -mylogger -INFO - 0
2019-01-22 17:51:52,490 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from TB_USER;" head=no file=T
B_USER.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,578 -mylogger -INFO - 0
2019-01-22 17:51:52,578 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from TEST;" head=no file=TEST
.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,665 -mylogger -INFO - 0
2019-01-22 17:51:52,665 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from XGJ;" head=no file=XGJ.d
at field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,771 -mylogger -INFO - 0
2019-01-22 17:51:52,771 -mylogger -INFO - 開始導出數(shù)據(jù): exportquery= sqluldr2 user=c##scott/tiger@192.168.1.250:1521/orcl query="select * from XGJ_2;" head=no file=XGJ
_2.dat field=0x03 record=0x030x0a safe=yes
2019-01-22 17:51:52,856 -mylogger -INFO - 0
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 配置python連接oracle讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫的操作流程
- 解決python3.6用cx_Oracle庫連接Oracle的問題
- Python連接Oracle之環(huán)境配置、實例代碼及報錯解決方法詳解
- 使用Python腳本zabbix自定義key監(jiān)控oracle連接狀態(tài)
- 解決python通過cx_Oracle模塊連接Oracle亂碼的問題
- Python3連接SQLServer、Oracle、MySql的方法
- Python3.6連接Oracle數(shù)據(jù)庫的方法詳解
- python cx_Oracle的基礎(chǔ)使用方法(連接和增刪改查)
- windows下python連接oracle數(shù)據(jù)庫
- python安裝oracle擴展及數(shù)據(jù)庫連接方法
- python連接oracle數(shù)據(jù)庫實例
- 教你使用Python連接oracle
相關(guān)文章
django 按時間范圍查詢數(shù)據(jù)庫實例代碼
這篇文章主要介紹了django 按時間范圍查詢數(shù)據(jù)庫實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02python筆記(1) 關(guān)于我們應(yīng)不應(yīng)該繼續(xù)學習python
關(guān)于Python,如果你要學習,建議大家查看一下網(wǎng)站:因為本人也是剛剛決定收集點零碎時間來學習下它,推薦可能并不是最好的2012-10-10Python 安裝 virturalenv 虛擬環(huán)境的教程詳解
這篇文章主要介紹了Python 安裝 virturalenv 虛擬環(huán)境的教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02python操作SqlServer獲取特定表的所有列名(推薦)
這篇文章主要介紹了python操作SqlServer獲取特定表的所有列名,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08