https://www.dgrt.cn/a/2259443.html?action=onClick
解决以下问题:
一、 Python连接Oracle数据库,并查询、提取Oracle数据库中数据?
url='jdbc:oracle:thin:@ip:端口:实例名'
user='username'
password='password'
dirver='oracle.jdbc.driver.OracleDriver'
jarFile='/opt/ojdbc6.jar'
sqlstr = '''select * from tablename'''
conn=jaydebeapi.connect(dirver,url,[user,password],jarFile)
df = pd.read_sql_query(sqlstr,conn)
conn.close()
二、 通过Python在Oracle数据库中创建表
url='jdbc:oracle:thin:@ip:端口:实例名'
user='username'
password='password'
dirver='oracle.jdbc.driver.OracleDriver'
jarFile='/opt/ojdbc6.jar'
conn=jaydebeapi.connect(dirver,url,[user,password],jarFile)
curs1=conn.cursor()
sqlStr1 = '''create table table tablename(
name varchar(10) ,
city varchar(10)
)'''
curs1.execute(sqlStr1)
curs1.close()
conn.close()
tablename——表名
name,city——列名
三、 Python数据插入到Oracle数据库中
df1 = pd.DataFrame(data = {name:[Lily,Jenny],city:[KunMing,ShangHai])
#把df1中数据插入到表tablename中
url='jdbc:oracle:thin:@ip:端口:实例名'
user='username'
password='password'
dirver='oracle.jdbc.driver.OracleDriver'
jarFile='/opt/ojdbc6.jar'
conn=jaydebeapi.connect(dirver,url,[user,password],jarFile)
curs1 = conn.cursor()
for i in range(len(df1)):sqlStr1 = '''insert into tablename values ('%s','%s'')'''%(df1.iloc[i][0],df1.iloc[i][1])
curs1.execute(sqlStr1)
curs1.close()
conn.close()
四、 Python删除Oracle数据库中数据
url='jdbc:oracle:thin:@ip:端口:实例名'
user='username'
password='password'
dirver='oracle.jdbc.driver.OracleDriver'
jarFile='/opt/ojdbc6.jar'
conn=jaydebeapi.connect(dirver,url,[user,password],jarFile)
curs1 = conn.cursor()
sqlStr1 = '''TRUNCATE table tablename'''
curs1.execute(sqlStr1)
curs1.close()
conn.close()