Python讀寫及備份oracle數(shù)據(jù)庫(kù)操作示例
本文實(shí)例講述了Python讀寫及備份oracle數(shù)據(jù)庫(kù)操作。分享給大家供大家參考,具體如下:
最近項(xiàng)目中需要用到Python調(diào)用oracle實(shí)現(xiàn)讀寫操作,踩過很多坑,歷盡艱辛終于實(shí)現(xiàn)了。性能怎樣先不說,有方法后面再調(diào)優(yōu)嘛?,F(xiàn)在把代碼和注意點(diǎn)記錄一下。
1. 所需Python工具庫(kù)
cx_Oracle,pandas,可以使用通過控制臺(tái)使用pip
進(jìn)行安裝(電腦中已經(jīng)安裝)
2. 實(shí)現(xiàn)查詢操作
#工具庫(kù)導(dǎo)入 import pandas as pd import cx_Oracle # 注:設(shè)置環(huán)境編碼方式,可解決讀取數(shù)據(jù)庫(kù)亂碼問題 import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' #實(shí)現(xiàn)查詢并返回dataframe def query(table) host = "127.0.0.1" #數(shù)據(jù)庫(kù)ip port = "1521" #端口 sid = "test" #數(shù)據(jù)庫(kù)名稱 dsn = cx_Oracle.makedsn(host, port, sid) #scott是數(shù)據(jù)用戶名,tiger是登錄密碼(默認(rèn)用戶名和密碼) conn = cx_Oracle.connect("scott", "tiger", dsn) #SQL語句,可以定制,實(shí)現(xiàn)靈活查詢 sql = 'select * from '+ table # 使用pandas 的read_sql函數(shù),可以直接將數(shù)據(jù)存放在dataframe中 results = pd.read_sql(sql,conn) conn.close return results test_data = query(test_table) # 可以得到結(jié)果集
3. 實(shí)現(xiàn)插入操作
#工具庫(kù)導(dǎo)入 import pandas as pd import cx_Oracle #實(shí)現(xiàn)插入功能 def input_to_db(data,table): host = "127.0.0.1" #數(shù)據(jù)庫(kù)ip port = "1521" #端口 sid = "test" #數(shù)據(jù)庫(kù)名稱 dsn = cx_Oracle.makedsn(host, port, sid) #scott是數(shù)據(jù)用戶名,tiger是登錄密碼(默認(rèn)用戶名和密碼) conn = cx_Oracle.connect("scott", "tiger", dsn) #建立游標(biāo) cursor = connection.cursor() #sql語句,注意%s要加引號(hào),否則會(huì)報(bào)ora-01036錯(cuò)誤 query = "INSERT INTO"+table+"(name,gender,age) VALUES ('%s', '%s', '%s')" #逐行插入數(shù)據(jù) for i in range(len(data)): name= data.ix[i,0] gender= data.ix[i,1] age= data.ix[i,2] # 執(zhí)行sql語句 cursor.execute(query % (name,gender,age)) connection.commit() # 關(guān)閉游標(biāo) cursor.close() connection.close() #測(cè)試插入數(shù)據(jù)庫(kù) #測(cè)試數(shù)據(jù)集 test_data = pd.DataFrame([['小明','男',18],['小芳','女',18]],index = [1,2],columns=['name','gender','age']) #調(diào)用函數(shù)實(shí)現(xiàn)插入 input_to_db(test_data,test_table1)
4. Python備份Oracle數(shù)據(jù)庫(kù)
#!/usr/bin/python #coding=utf-8 import threading import os import time #用戶名 user = 'username' #密碼 passwd = 'password' #備份保存路徑 savepath = '/home/oracle/orcl_bak/' #要備份的表 tables = ' tables=department,employee' #備份周期 circle = 2.0 #備份命令 global bak_command bak_command = 'exp '+user+'/'+passwd + ' file=' + savepath def orclBak(): now = time.strftime('%Y-%m-%d %H:%M:%S') command = bak_command + now + '.dmp' + tables print command if os.system(command) == 0: print '備份成功' else: print '備份失敗' global t t = threading.Timer(circle, orclBak) t.start() t = threading.Timer(circle, orclBak) t.start()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫(kù)操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送
- Python實(shí)現(xiàn)備份MySQL數(shù)據(jù)庫(kù)的方法示例
- Python腳本實(shí)現(xiàn)自動(dòng)將數(shù)據(jù)庫(kù)備份到 Dropbox
- python備份文件以及mysql數(shù)據(jù)庫(kù)的腳本代碼
- python使用多線程查詢數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
- Python基于多線程操作數(shù)據(jù)庫(kù)相關(guān)問題分析
- Python基于多線程實(shí)現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫(kù)的方法
- python使用多線程備份數(shù)據(jù)庫(kù)的步驟
相關(guān)文章
Tensorflow中的圖(tf.Graph)和會(huì)話(tf.Session)的實(shí)現(xiàn)
這篇文章主要介紹了Tensorflow中的圖(tf.Graph)和會(huì)話(tf.Session)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python正規(guī)則表達(dá)式學(xué)習(xí)指南
本文介紹了Python對(duì)于正則表達(dá)式的支持,包括正則表達(dá)式基礎(chǔ)以及Python正則表達(dá)式標(biāo)準(zhǔn)庫(kù)的完整介紹及使用示例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友一起學(xué)習(xí)吧2016-08-08Python實(shí)戰(zhàn)之外星人入侵游戲示例代碼
這篇文章主要介紹了利用Python編寫的外星人入侵游戲的示例代碼,文中的代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以學(xué)習(xí)一下2022-01-01利用Python通過獲取剪切板數(shù)據(jù)實(shí)現(xiàn)百度劃詞搜索功能
大家是不是嫌棄每次打開百度太麻煩?今天教大家利用Python通過獲取剪切板數(shù)據(jù)實(shí)現(xiàn)百度劃詞搜索功能,用程序直接打開網(wǎng)頁,需要的朋友可以參考下2021-06-06基于Python實(shí)現(xiàn)批量縮放圖片(視頻)尺寸
這篇文章主要為大家詳細(xì)介紹了如何通過Python語言實(shí)現(xiàn)批量縮放圖片(視頻)尺寸的功能,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03