使用cx_oracle操作oracle数据库
oracleClient 包
#!/user/bin env python # author:Simple-Sir # create_time: 2021/11/9 15:41 import cx_Oracle class OracleClient(): def __init__(self,sql_list): self.sql_list=sql_list def run_sql(self): try: conn = cx_Oracle.connect('用户/密码@ip/数据库') curs = conn.cursor() print('Connect to Oracle database successed!') except Exception as e: print('Connect to Oracle database failed!') for i in self.sql_list: curs.execute(i) conn.commit() # 提交 print(i.strip()) print('-' * 50) print('execute successed') print('-'*50) curs.close() conn.close() oracleClient.py
引用oracleClient包,直接使用
#!/user/bin env python # author:Simple-Sir # create_time: 2021/11/9 15:41 import cx_Oracle import sys # 导入环境目录 sys.path.append('oracleClient包所在目录') # 导入oracle客户端包 from oracleClient import OracleClient # 只需要修改下面这一段需要执行的SQL,即可。 sql_list = [] sql_list.append(''' drop table py_test ''') sql_list.append(''' create table py_test( id int, name varchar2(10) ) ''') sql_list.append(''' insert into py_test values(1,'oracle') ''') sql_list.append(''' select * from py_test ''') if __name__ == '__main__': runsql = OracleClient(sql_list) runsql.run_sql() # 执行SQL