如果你不想使用数据库链接、存储过程,并且希望使用 Python 来将 Oracle 数据库中的查询结果存储到另一个数据库,可以使用以下步骤:
在 Python 中连接到 A 库和 B 库,并执行查询操作。 - import cx_Oracle
- # 连接到 A 库
- a_conn = cx_Oracle.connect("a_username/a_password@a_db_host:a_db_port/a_db_sid")
- a_cursor = a_conn.cursor()
- # 执行查询操作
- a_cursor.execute("SELECT column1, column2, column3 FROM a_table")
- # 获取查询结果
- query_result = a_cursor.fetchall()
- # 关闭游标和连接
- a_cursor.close()
- a_conn.close()
复制代码在这个示例中,我们使用 cx_Oracle 库连接到 A 库,并执行了查询操作。你需要替换 a_username、a_password、a_db_host、a_db_port、a_db_sid、a_table 和列名为你自己的实际值。
将查询结果存储到 B 库。 - # 连接到 B 库
- b_conn = cx_Oracle.connect("b_username/b_password@b_db_host:b_db_port/b_db_sid")
- b_cursor = b_conn.cursor()
- # 插入查询结果到 B 库的表
- for row in query_result:
- b_cursor.execute("INSERT INTO b_table (column1, column2, column3) VALUES (:1, :2, :3)", row)
- # 提交事务和关闭游标、连接
- b_conn.commit()
- b_cursor.close()
- b_conn.close()
复制代码在这个示例中,我们使用 cx_Oracle 库连接到 B 库,并将查询结果逐行插入到 B 库的表中。你需要替换 b_username、b_password、b_db_host、b_db_port、b_db_sid、b_table 和列名为你自己的实际值。
这是一种使用 Python 来将 Oracle 数据库中的查询结果存储到另一个数据库的方法。你可以根据实际情况进行适当的修改和调整,例如添加错误处理、参数化查询等。确保在使用之前安装了 cx_Oracle 库,并且配置了正确的连接参数。 |