import cx_Oracle
import pandas as pd
########## Python 连接 Oracle 并执行语句########
dsn_tns = cx_Oracle.makedsn('100.100.100.100', '1521', service_name='hrdb') # 数据库信息,注意是service_name,非SID
conn = cx_Oracle.connect(user='your admin', password='your password', dsn=dsn_tns) # 用户信息
c = conn.cursor() #创建连接
c.execute("select * from xxx") #查询语句
res = c.fetchall()
print(res) #打印所有
for i in res:
print(i[2],i[3]) #循环打印特定列
c.close()
#########Python 读取Excel 并插入到Oracle #####
excel_data = pd.read_excel('xxxxxx.xlsx',header= [0])
x = excel_data.values.tolist()
for i,row in excel_data.iterrows():
#print(tuple(row))
sql = "INSERT INTO YOURTABLE (ID1,ID2,ID3,ID4) VALUES(:1,:2,:3,:4)" ##ID1 ID2 .. 为表列名字
c.execute(sql, tuple(row))
conn.commit()
c.close()