📜  Python中的 Oracle 数据库连接

📅  最后修改于: 2022-05-13 01:55:28.348000             🧑  作者: Mango

Python中的 Oracle 数据库连接

有时作为编程的一部分,我们需要使用数据库,因为我们要存储大量信息,所以我们使用数据库,例如 Oracle、MySQL 等。所以在本文中,我们将讨论 Oracle 数据库的连接性使用Python。这可以通过模块名称cx_Oracle来完成。

甲骨文数据库
为了通过我们的Python程序与任何数据库进行通信,我们需要一些连接器,它不过是cx_Oracle模块。

安装 cx-Oracle :

如果您使用Python >= 3.6,请在 Linux 中使用以下命令:–

pip install cx-Oracle

如果您使用Python >= 3.6,请在 Windows 中使用以下命令:-

py -m pip install cx-Oracle

通过此命令,您可以安装 cx-Oracle 软件包,但需要先在您的 PC 上安装 Oracle 数据库。

  • 导入数据库特定模块
    前任。导入 cx_Oracle
  • connect():现在使用 connect()函数在Python程序和 Oracle 数据库之间建立连接。
con = cx_Oracle.connect('username/password@localhost')
  • cursor():要执行 SQL 查询并提供结果,需要一些特殊对象,即 cursor() 对象。
cursor = cx_Oracle.cursor()
  • 执行/executemany 方法:
  • commit():用于包含更新、插入、删除等操作的 DML(数据操作语言)查询。我们需要 commit() 然后只有结果反映在数据库中。
  • fetchone()、fetchmany(int)、fetchall():
    1. fetchone() :此方法用于从结果集的顶部获取一行。
    2. fetchmany(int):此方法用于根据传入的参数获取有限数量的行。
    3. fetchall() :此方法用于从结果集中获取所有行。
  • close():完成后必须关闭所有操作。
cursor.close()
con.close()

SQL语句的执行:

1. 表的创建

Python3
# importing module
import cx_Oracle
 
# Create a table in Oracle database
try:
 
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
    print(con.version)
 
    # Now execute the sqlquery
    cursor = con.cursor()
 
    # Creating a table employee
    cursor.execute("create table employee(empid integer primary key, name varchar2(30), salary number(10, 2))")
 
    print("Table Created successfully")
 
except cx_Oracle.DatabaseError as e:
    print("There is a problem with Oracle", e)
 
# by writing finally if any error occurs
# then also we can close the all database operation
finally:
    if cursor:
        cursor.close()
    if con:
        con.close()


Python3
# importing module
import cx_Oracle
 
# Inserting a record into a table in Oracle database
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
    cursor = con.cursor()
     
    #con.autocommit = True
    # Inserting a record into table employee
    cursor.execute('insert into employee values(10001,\'Rahul\',50000.50)')
 
    # commit() to make changes reflect in the database
    con.commit()
    print('Record inserted successfully')
 
except cx_Oracle.DatabaseError as e:
    print("There is a problem with Oracle", e)
 
# by writing finally if any error occurs
# then also we can close the all database operation
finally:
    if cursor:
        cursor.close()
    if con:
        con.close()


Python3
import cx_Oracle
 
# Load data from a csv file into Oracle table using executemany
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is an error in Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
        data = [[10007, 'Vikram', 48000.0], [10008, 'Sunil', 65000.1], [10009, 'Sameer', 75000.0]]
 
        cur = con.cursor()
        # Inserting multiple records into employee table
        # (:1,:2,:3) are place holders. They pick data from a list supplied as argument
        cur.executemany('insert into employee values(:1,:2,:3)', data)
 
    except cx_Oracle.DatabaseError as er:
        print('There is an error in Oracle database:', er)
 
    except Exception as er:
        print(er)
 
    else:
        # To commit the transaction manually
        con.commit()
        print('Multiple records are inserted successfully')
 
finally:
    if cur:
        cur.close()
    if con:
        con.close()


Python3
import cx_Oracle
 
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is an error in the Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
 
        # fetchall() is used to fetch all records from result set
        cur.execute('select * from employee')
        rows = cur.fetchall()
        print(rows)
 
        # fetchmany(int) is used to fetch limited number of records from result set based on integer argument passed in it
        cur.execute('select * from employee')
        rows = cur.fetchmany(3)
        print(rows)
 
        # fetchone() is used fetch one record from top of the result set
        cur.execute('select * from employee')
        rows = cur.fetchone()
        print(rows)
 
    except cx_Oracle.DatabaseError as er:
        print('There is an error in the Oracle database:', er)
 
    except Exception as er:
        print('Error:'+str(er))
 
    finally:
        if cur:
            cur.close()
 
finally:
    if con:
        con.close()


Python3
import cx_Oracle
 
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is error in the Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
 
        cur.execute('select * from employee where salary > :sal', {'sal': 50000})
        rows = cur.fetchall()
        print(rows)
 
    except cx_Oracle.DatabaseError as er:
        print('There is error in the Oracle database:', er)
 
    except Exception as er:
        print('Error:', er)
 
    finally:
        if cur:
            cur.close()
 
finally:
    if con:
        con.close()


输出:

Table Created successfully

DDL 语句不需要提交。它们是自动提交的。在上面的程序中,我使用了 execute() 方法来执行一条 SQL 语句。

2. 使用 execute() 方法向表中插入记录

Python3

# importing module
import cx_Oracle
 
# Inserting a record into a table in Oracle database
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
    cursor = con.cursor()
     
    #con.autocommit = True
    # Inserting a record into table employee
    cursor.execute('insert into employee values(10001,\'Rahul\',50000.50)')
 
    # commit() to make changes reflect in the database
    con.commit()
    print('Record inserted successfully')
 
except cx_Oracle.DatabaseError as e:
    print("There is a problem with Oracle", e)
 
# by writing finally if any error occurs
# then also we can close the all database operation
finally:
    if cursor:
        cursor.close()
    if con:
        con.close()

输出:

Record inserted successfully

一旦我们执行任何 DML 语句,就需要提交事务。您可以通过 2 种方式提交事务:–

  1. con.commit()。这用于手动提交事务。
  2. con.autocommit = 真。这用于自动提交事务。

3.使用 executemany() 方法向表中插入多条记录

Python3

import cx_Oracle
 
# Load data from a csv file into Oracle table using executemany
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is an error in Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
        data = [[10007, 'Vikram', 48000.0], [10008, 'Sunil', 65000.1], [10009, 'Sameer', 75000.0]]
 
        cur = con.cursor()
        # Inserting multiple records into employee table
        # (:1,:2,:3) are place holders. They pick data from a list supplied as argument
        cur.executemany('insert into employee values(:1,:2,:3)', data)
 
    except cx_Oracle.DatabaseError as er:
        print('There is an error in Oracle database:', er)
 
    except Exception as er:
        print(er)
 
    else:
        # To commit the transaction manually
        con.commit()
        print('Multiple records are inserted successfully')
 
finally:
    if cur:
        cur.close()
    if con:
        con.close()

输出:

Multiple records are inserted successfully

有时可能需要根据每次提供给它的不同值多次执行 SQL 语句。这可以使用 executemany() 方法来实现。我们提供一个包含值列表的列表,这些值将替换要执行的 SQL 查询中的占位符。

从上面的案例

  • :1 被值 10007 替换
  • :2 被值 'Vikram' 取代
  • :3 被值 48000.0 替换

依此类推(给定列表中的下一个值列表)

同样,您可以提供字典列表。但是我们将使用绑定变量而不是占位符(稍后讨论)。

4. 使用 fetchall()、fetchmany(int)、fetchone() 从选择查询中查看结果集

Python3

import cx_Oracle
 
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is an error in the Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
 
        # fetchall() is used to fetch all records from result set
        cur.execute('select * from employee')
        rows = cur.fetchall()
        print(rows)
 
        # fetchmany(int) is used to fetch limited number of records from result set based on integer argument passed in it
        cur.execute('select * from employee')
        rows = cur.fetchmany(3)
        print(rows)
 
        # fetchone() is used fetch one record from top of the result set
        cur.execute('select * from employee')
        rows = cur.fetchone()
        print(rows)
 
    except cx_Oracle.DatabaseError as er:
        print('There is an error in the Oracle database:', er)
 
    except Exception as er:
        print('Error:'+str(er))
 
    finally:
        if cur:
            cur.close()
 
finally:
    if con:
        con.close()

输出:

[(10001, 'Rahul', 50000.5), (10002, 'Sanoj', 40000.75), (10003, 'Soumik', 30000.25), (10004, 'Sayan', 45000.0), (10005, 'Sobhan', 60000.1), (10006, 'Gururaj', 70000.0), (10007, 'Vikram', 48000.0), (10008, 'Sunil', 65000.1), (10009, 'Sameer', 75000.0)]
[(10001, 'Rahul', 50000.5), (10002, 'Sanoj', 40000.75), (10003, 'Soumik', 30000.25)]
(10001, 'Rahul', 50000.5)

在上面的程序中,我们使用了 3 种方法

  1. fetchall() : fetchall()用于从结果集中获取所有记录。
  2. fetchmany(int) : fetchmany(int)用于根据传入的整数参数从结果集中获取有限数量的记录。
  3. fetchone() : fetchone()用于从结果集的顶部获取一条记录。

5. 使用绑定变量从选择查询中查看结果集

Python3

import cx_Oracle
 
try:
    con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
 
except cx_Oracle.DatabaseError as er:
    print('There is error in the Oracle database:', er)
 
else:
    try:
        cur = con.cursor()
 
        cur.execute('select * from employee where salary > :sal', {'sal': 50000})
        rows = cur.fetchall()
        print(rows)
 
    except cx_Oracle.DatabaseError as er:
        print('There is error in the Oracle database:', er)
 
    except Exception as er:
        print('Error:', er)
 
    finally:
        if cur:
            cur.close()
 
finally:
    if con:
        con.close()

输出:

[(10001, 'Rahul', 50000.5), (10005, 'Sobhan', 60000.1), (10006, 'Gururaj', 70000.0),
 (10008, 'Sunil', 65000.1), (10009, 'Sameer', 75000.0)]

在这种情况下,我在 execute() 方法中传递了一个字典。该字典包含绑定变量的名称作为键,以及它的对应值。执行 SQL 查询时,键中的值被替换为绑定变量。