Python讀寫(xiě)及備份oracle數(shù)據(jù)庫(kù)操作示例
本文實(shí)例講述了Python讀寫(xiě)及備份oracle數(shù)據(jù)庫(kù)操作。分享給大家供大家參考,具體如下:
最近項(xiàng)目中需要用到Python調(diào)用oracle實(shí)現(xiàn)讀寫(xiě)操作,踩過(guò)很多坑,歷盡艱辛終于實(shí)現(xiàn)了。性能怎樣先不說(shuō),有方法后面再調(diào)優(yōu)嘛?,F(xiàn)在把代碼和注意點(diǎn)記錄一下。
1. 所需Python工具庫(kù)
cx_Oracle,pandas,可以使用通過(guò)控制臺(tái)使用pip進(jìn)行安裝(電腦中已經(jīng)安裝)

2. 實(shí)現(xiàn)查詢(xún)操作
#工具庫(kù)導(dǎo)入
import pandas as pd
import cx_Oracle
# 注:設(shè)置環(huán)境編碼方式,可解決讀取數(shù)據(jù)庫(kù)亂碼問(wèn)題
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
#實(shí)現(xiàn)查詢(xún)并返回dataframe
def query(table)
host = "127.0.0.1" #數(shù)據(jù)庫(kù)ip
port = "1521" #端口
sid = "test" #數(shù)據(jù)庫(kù)名稱(chēng)
dsn = cx_Oracle.makedsn(host, port, sid)
#scott是數(shù)據(jù)用戶名,tiger是登錄密碼(默認(rèn)用戶名和密碼)
conn = cx_Oracle.connect("scott", "tiger", dsn)
#SQL語(yǔ)句,可以定制,實(shí)現(xiàn)靈活查詢(xú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ù)名稱(chēng)
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語(yǔ)句,注意%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語(yǔ)句
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)容感興趣的讀者可查看本站專(zhuān)題:《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(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使用多線程查詢(xún)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
- Python基于多線程操作數(shù)據(jù)庫(kù)相關(guān)問(wèn)題分析
- Python基于多線程實(shí)現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫(kù)的方法
- python使用多線程備份數(shù)據(jù)庫(kù)的步驟
相關(guān)文章
tensorflow模型保存、加載之變量重命名實(shí)例
今天小編就為大家分享一篇tensorflow模型保存、加載之變量重命名實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python WXPY實(shí)現(xiàn)微信監(jiān)控報(bào)警功能的代碼
本篇文章主要介紹了Python WXPY實(shí)現(xiàn)微信監(jiān)控報(bào)警功能的代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Python實(shí)現(xiàn)PS濾鏡的萬(wàn)花筒效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡的萬(wàn)花筒效果,結(jié)合實(shí)例形式分析了Python基于skimage模塊操作圖片實(shí)現(xiàn)PS濾鏡萬(wàn)花筒效果的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
python爬蟲(chóng) urllib模塊發(fā)起post請(qǐng)求過(guò)程解析
這篇文章主要介紹了python爬蟲(chóng) urllib模塊發(fā)起post請(qǐng)求過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python?pygame派生精靈和精靈組創(chuàng)建敵機(jī)
這篇文章主要為大家介紹了Python?pygame派生精靈和精靈組創(chuàng)建敵機(jī)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
對(duì)pandas中時(shí)間窗函數(shù)rolling的使用詳解
今天小編就為大家分享一篇對(duì)pandas中時(shí)間窗函數(shù)rolling的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python實(shí)現(xiàn)自動(dòng)化整理文件的示例代碼
這篇文章主要介紹了如何通過(guò)Python編程完成文件的自動(dòng)分類(lèi)、文件和文件夾的快速查找、重復(fù)文件的清理、圖片格式的轉(zhuǎn)換等常見(jiàn)工作,需要的可以參考一下2022-09-09
解決keras,val_categorical_accuracy:,0.0000e+00問(wèn)題
這篇文章主要介紹了解決keras,val_categorical_accuracy:,0.0000e+00問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

