如何使用Python连接和操作Oracle数据库?

  • Post category:Python

在Python中,可以使用cx_Oracle模块连接和操作Oracle数据库。以下是Python使用cx_Oracle模块连接和操作Oracle数据库的完整攻略,包括连接Oracle数据库、插入数据、查询、更新数据和删除数据等操作。

连接Oracle数据库

在Python中,可以使用cx_Oracle模块连接Oracle数据库。以下是连接Oracle数据库的基本语法:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)

在上面的语法中,cx_Oracle.makedsn()方法用于创建DSN,localhost是Oracle服务器的主机名,1521是Oracle服务器的端口号,ORCLCDB是要连接的数据库名称,myusername是数据库的用户名,mypassword是连接数据库的密码。

插入数据

在Oracle中可以使用INSERT INTO语句插入数据。以下是插入数据的基本语法:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 插入一条数据
cur.execute("INSERT INTO mytable (column1, column2) VALUES (:1, :2)", ("value1", "value2"))

# 插入多条数据
mylist = [
  ("value1", "value2"),
  ("value3", "value4"),
  ("value5", "value6")
]
cur.executemany("INSERT INTO mytable (column1, column2) VALUES (:1, :2)", mylist)

# 提交更改
conn.commit()

在上面的语法中,cur.execute()方法用于插入一条数据,cur.executemany()方法用于插入多条数据,conn.commit()方法用于提交更改。

查询数据

在Oracle中,可以使用SELECT语句查询数据。以下是查询数据的基本语法:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 查询所有数据
cur.execute("SELECT * FROM mytable")
rows = cur.fetchall()

# 查询指定条件的数据
cur.execute("SELECT * FROM mytable WHERE column1 = :1", ("value1",))
rows = cur.fetchall()

在上面的语法中,cur.execute()方法用于执行查询,cur.fetchall()方法用于获取查询结果。

更新数据

在Oracle中,可以使用UPDATE语句更新数据。以下是更新数据的基本语法:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 更新数据
cur.execute("UPDATE mytable SET column1 = :1 WHERE column2 = :2", ("new_value", "old_value"))

# 提交更改
conn.commit()

在上面的语法中,cur.execute()方法用于更新数据,conn.commit()方法用于提交更改。

删除数据

在Oracle中,可以使用DELETE语句删除数据。以下是删除数据的基本语法:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 删除数据
cur.execute("DELETE FROM mytable WHERE column1 = :1", ("value1",))

# 提交更改
conn.commit()

在上面的语法中,cur.execute()方法用于删除数据,conn.commit()方法用于提交更改。

示例1

在这个示例中,我们将使用Python连接到Oracle数据库,并插入一些数据,然后查询所有数据并打印结果。

以下是Python代码:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 插入数据
cur.execute("INSERT INTO mytable (column1, column2) VALUES (:1, :2)", ("value1", "value2"))

# 查询所有数据
cur.execute("SELECT * FROM mytable")
rows = cur.fetchall()

# 打印查询结果
for row in rows:
    print(row)

在上面的代码中,我们使用cx_Oracle模块连接到Oracle数据库。然后,我们使用cur.execute()方法插入一条数据。最后,我们使用cur.fetchall()方法获取查询结果,使用for循环遍历每一行,并使用print()函数打印每一行。

示例2

在这个示例中,我们将使用Python连接到Oracle数据库,并更新一些数据,然后查询指定条件的数据并打印结果。

以下是Python代码:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='ORCLCDB')
conn = cx_Oracle.connect(user='myusername', password='mypassword', dsn=dsn_tns)
cur = conn.cursor()

# 更新数据
cur.execute("UPDATE mytable SET column1 = :1 WHERE column2 = :2", ("new_value", "old_value"))

# 查询指定条件的数据
cur.execute("SELECT * FROM mytable WHERE column1 = :1", ("new_value",))
rows = cur.fetchall()

# 打印查询结果
for row in rows:
    print(row)

在上面的代码中,我们使用cx_Oracle模块连接到Oracle数据库。然后,我们使用cur.execute()方法更新一条数据。最后,我们使用cur.fetchall()方法获取查询结果,使用for循环遍历每一行,并使用print()函数打印每一行。

以上是使用Python连接和操作Oracle数据库的完整攻略,包括连接Oracle数据库、插入数据、查询数据、更新数据和删除数据等操作。