# 导入cx_Oracle库,用于连接Oracle数据库
import cx_Oracle
# 定义连接参数
dsn_tns = cx_Oracle.makedsn('hostname', 'port', service_name='service_name') # 替换为实际的主机名、端口和服务名
user = 'your_username' # 替换为你的用户名
password = 'your_password' # 替换为你的密码
# 建立连接
connection = cx_Oracle.connect(user=user, password=password, dsn=dsn_tns)
# 创建游标对象
cursor = connection.cursor()
# 执行查询语句
cursor.execute('SELECT * FROM your_table') # 替换为你要查询的表名
# 获取查询结果
rows = cursor.fetchall()
for row in rows:
print(row)
# 关闭游标和连接
cursor.close()
connection.close()
cx_Oracle 库来连接 Oracle 数据库。cx_Oracle.makedsn 函数创建数据源名称 (DSN),并指定主机名、端口和服务名。然后定义用户名和密码。cx_Oracle.connect 方法建立与数据库的连接。connection.cursor() 创建一个游标对象,用于执行 SQL 语句。cursor.execute 方法执行 SQL 查询语句。cursor.fetchall() 获取查询结果,并遍历打印每一行数据。请根据实际情况替换代码中的占位符(如 hostname、port、service_name、your_username、your_password 和 your_table)。
上一篇:python or
下一篇:python heapq
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站