python3连接oracle并打印sql结果
import cx_Oracle# 数据库连接信息
db_host = 'localhost' # 数据库主机名
db_port = 1521 # 数据库端口号
db_service = 'ORCL' # 数据库服务名
db_user = 'your_username' # 数据库用户名
db_password = 'your_password' # 数据库密码
# 连接到Oracle数据库
dsn = cx_Oracle.makedsn(db_host, db_port, service_name=db_service)
conn = cx_Oracle.connect(user=db_user, password=db_password, dsn=dsn)
# 创建游标
cursor = conn.cursor()
# 执行SQL查询语句
sql = "SELECT * FROM your_table"
cursor.execute(sql)
# 获取查询结果
result = cursor.fetchall()
# 打印查询结果
for row in result:
print(row)
# 关闭游标和数据库连接
cursor.close()
conn.close()
页:
[1]