Python3-cx_Oracle模块-数据库操作之Oracle
模块安装
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()
import cx_Oracle conn = None cursor = None try: # 1.连接数据库 conn = cx_Oracle.connect("username/password@13.13.13.123:1521/orcl") # 2.创建游标对象 cursor = conn.cursor() # 3.执行删除语句 sql = "DELETE FROM TAB_STUDENT WHERE ID = :id" sql_args = {"id": 30} cursor.execute(sql, sql_args) print(cursor.rowcount) # 获取受影响的行数 # 4.提交事务 conn.commit() except Exception as ex: pass finally: # 5.关闭游标与连接 cursor.close() conn.close()
import cx_Oracle conn = None cursor = None try: # 1.连接数据库 conn = cx_Oracle.connect("username/password@13.13.13.123:1521/orcl") # 2.创建游标对象 cursor = conn.cursor() # 3.执行更新语句 sql = "UPDATE TAB_STUDENT SET NAME = :new_name WHERE ID = :id" sql_args = {"id": 1, "new_name": "Jack"} cursor.execute(sql, sql_args) print(cursor.rowcount) # 获取受影响的行数 # 4.提交事务 conn.commit() except Exception as ex: pass finally: # 5.关闭游标与连接 cursor.close() conn.close()
import cx_Oracle conn = None cursor = None try: # 1.连接数据库 conn = cx_Oracle.connect("username/password@13.13.13.123/orcl") # 2.创建一个游标对象Cursor对象 cursor = conn.cursor() # 3.执行查询语句 sql = "SELECT * FROM TAB_STUDENT WHERE USER_NAME=:user_name AND MOBILE=:mobile" sql_args = {"user_name": "qq1207501666", "mobile": 18888888888} cursor.execute(sql, sql_args) # 4.提取查询结果 print(cursor.fetchone()) # 提取一行结果,相当于cursor.next() # cursor.fetchmany(3) # 提取3行结果 # cursor.fetchall() # 提取全部结果 except Exception as ex: pass finally: # 8.关闭游标与连接 cursor.close() conn.close()
# 1.使用"命名参数"进行绑定 sql = "SELECT * FROM TAB_STUDENT WHERE ID = :ID" sql_args = {"ID": 1} cursor.execute(sql, sql_args) # 传入的是一个字典 # 2.使用"位置参数"进行绑定 sql = "SELECT * FROM TAB_STUDENT WHERE ID = :1" sql_args = (888,) cursor.execute(sql, sql_args) # 传入的是一个元组
posted on 2017-04-27 15:13 AustralGeek 阅读(932) 评论(0) 编辑 收藏 举报