python operating Oracle database

According to the query: If python wants to operate the Oracle database, it needs to rely on the package to achieve the effect of the connected Oracle.
Step 1: Install the dependency

pip install cx-Oracle==8.0.1

Step 2: Connect

import cx_Oracle


def connection_oracle(connect_str, oracle_sql):
    """
    Oracle连接执行sql并返回数据
    :param connect_str: 连接字符串
    :param oracle_sql: Oracle的sql语句
    :return:
    """
    conn = cx_Oracle.connect(connect_str)
    # 使用cursor()方法获取操作游标
    curs = conn.cursor()
    # 执行sql
    curs.execute(oracle_sql)
    row = curs.fetchall()
    return row

The third step: the construction of the connection string

connect_str = '用户名/密码@IP:端口号/SERVICE_NAME'

Guess you like

Origin blog.csdn.net/qq_42631707/article/details/112983723