pip install cx_Oracle
接下来,你可以使用以下Python代码进行数据库连接:
import cx_Oracle
# 替换以下信息为你的实际数据库信息
db_username = 'your_username'
db_password = 'your_password'
db_host = 'your_host'
db_port = 'your_port'
db_service = 'your_service_name_or_sid'
# 构建连接字符串
connection_str = f'{db_username}/{db_password}@{db_host}:{db_port}/{db_service}'
# 尝试建立连接
try:
# 使用cx_Oracle.connect()方法建立连接
connection = cx_Oracle.connect(connection_str)
print("成功连接到Oracle数据库!")
# 在这里可以执行数据库操作
# 关闭连接
connection.close()
except cx_Oracle.Error as error:
print(f"连接Oracle数据库时发生错误: {error}")
请替换示例中的占位符(your_username,your_password等)为你的实际数据库信息。此代码使用cx_Oracle.connect()方法建立与Oracle数据库的连接。如果连接成功,将打印成功的消息;如果发生错误,将打印错误信息。
请注意,为了运行此代码,你需要有相应的Oracle数据库账户信息,并且Python环境中已安装cx_Oracle模块。
转载请注明出处:http://www.zyzy.cn/article/detail/8055/Oracle