方法一 通过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)