模块安装
1.安装cx_Oracle模块之前必须要安装Oracle客户端,否则无法使用
2.系统上需要装有对应版本的c++编译套件(Linux下:g++ Windows下:VC++)
参考文档
https://oracle.github.io/python-cx_Oracle/
http://cx-oracle.readthedocs.io/en/5.3/index.html
代码示例
import cx_Oracle conn = None cursor = None try: # 1.连接数据库 # conn = cx_Oracle.Connection("username", "password", "13.13.13.123:1521/orcl") # 或 conn = cx_Oracle.connect("username/password@13.13.13.123:1521/orcl") # 2.创建游标对象 cursor = conn.cursor() # 3.执行插入语句 sql = "INSERT INTO TAB_STUDENT(ID,NAME,AGE) VALUES(:id, :name,:age)" sql_args = {"id": 30, "name": "Jet", "age": 18} cursor.execute(sql, sql_args) print(cursor.rowcount) # 3.批量执行插入语句 # sql = "INSERT INTO TAB_STUDENT(ID,NAME,AGE) VALUES(:1, :2, :3)" # sql_args = [ # (10, "Jesdsat", 18,), # (11, "Jedsadt", 18,), # (12, "Jeadst", 18,), # (13, "Jedast", 18,), # ] # cursor.executemany(sql, sql_args) # 执行多条插入语句的方法 sql = "INSERT INTO TAB_STUDENT(ID,NAME,AGE) VALUES(:id, :name, :age)" sql_args = [ {"id": 20, "name": "Jet", "age": 18}, {"id": 21, "name": "Jet", "age": 18}, {"id": 22, "name": "Jet", "age": 18}, {"id": 23, "name": "Jet", "age": 18}, {"id": 24, "name": "Jet", "age": 18}, ] cursor.executemany(sql, sql_args) # 执行多条插入语句的方法 print(cursor.rowcount) # 获取受影响的行数 # 4.提交事务 conn.commit() except Exception as ex: pass finally: # 5.关闭游标与连接 cursor.close() conn.close()