python dataframe oracle
阿新 • • 發佈:2018-11-26
方法一 通過dataframe 存入oracle
#直接將dataframe 存入oracle cx_Oracle 6.0b1處於測試階段,我建議使用官方發行版5.3。 import cx_Oracle conn_string='oracle+cx_oracle://xxx:[email protected]:1234/xxx' from sqlalchemy import create_engine yconnect = create_engine(conn_string,echo=False) new_data.to_sql('ABC', con=conn_string,if_exists='append',index_label='id')#將檔案直接寫入oracle,只能寫入mysql,不能寫入oracle engine.excute("SELECT * FROM ABC").fetchall() #參考連結 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html
方法二 通過csv檔案存入oracle
#將資料以dataframe的形式存入csv
new_data=df(new_data)
csv_name = 'CLUSTER_RESULTS.csv'
new_data.to_csv(csv_name,encoding='gbk')
#已經建立好資料庫, #本打算通過python建立資料庫,但是,python不能執行多條sql語句,只能通過sql指令碼建立資料庫。 # coding=utf-8 import cx_Oracle import csv import xlrd import os import datetime class ImportOracle(object): def inoracle(self): pass def connOracle(self): os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.utf8' conn= cx_Oracle.connect("XXX", "XXX", '192.168.1.123:1234/XXXX') cursor=conn.cursor() sql='select * from %s'%(self.table_name) cursor.execute(sql) try: sql='truncate table %s'%(self.table_name) cursor.execute(sql) except cx_Oracle.Error as e: print(e) print("......") finally: a=[':%s'%i for i in range(len(self.title)+1)] value=','.join(a[1:]) sql='insert into %s values(%s)'%(self.table_name,value) cursor.prepare(sql) cursor.executemany(None, self.data) cursor.close() conn.commit() conn.close() class ImportOracleCsv(ImportOracle): def inoracle(self): with open(self.filename,'r') as f: reader=csv.reader(f) contents=[i for i in reader] title=contents[0] data=contents[1:] return (title,data) class ImportOracleExcel(ImportOracle): def inoracle(self): wb=xlrd.open_workbook(self.filename) sheet1=wb.sheet_by_index(0) title=sheet1.row_values(0) data=[sheet1.row_values(row) for row in range(1,sheet1.nrows)] return (title,data) class ImportError(ImportOracle): def inoracle(self): print('Undefine file type') return 0 class ChooseFactory(object): choose={} choose['csv']=ImportOracleCsv() choose['xlsx']=ImportOracleExcel() choose['xls']=ImportOracleExcel() def choosefile(self,ch): if ch in self.choose: op=self.choose[ch] else: op=ImportError() return op if __name__=="__main__": file_name=csv_name table_name='CBA' op=file_name.split('.')[-1] factory=ChooseFactory() cal=factory.choosefile(op) cal.filename=file_name (cal.title,cal.data)=cal.inoracle() cal.table_name=table_name cal.connOracle() os.remove(csv_name)