我有一个
Oracle PL/SQL
程序,我可以直接调用如下,没有问题:
BEGIN example_package_name.example_procedure(p_item_no => 123456, p_send_now => true); END;
(注:
p_item_no
预计会出现 NUMBER
,p_send_now
预计会出现 BOOLEAN
)
我正在尝试从我的
Robot Framework
测试自动化框架内运行它,如下所示。
首先,我有一个用于
robotframework-databaselibrary
的小辅助包装方法:
Execute SQL Store Procedure
[Arguments]
... ${target_database}
... ${target_store_procedure}
... ${store_procedure_arguments}
... ${timeout}=5 minutes
[Documentation] Small wrapper around DatabaseLibrary for the: Call Stored Procedure keyword.
[Timeout] ${timeout}
__Open connection to target database ${target_database}
DatabaseLibrary.Set Auto Commit autoCommit=${True}
DatabaseLibrary.Call Stored Procedure ${target_store_procedure} ${store_procedure_arguments}
Close connection from the current database
接下来,我的测试将尝试如下:
${item_no_int}= Convert To Integer ${test_item_dictionary.item_no}
${example_procedure_argument_list}= Create List p_item_no => ${item_no_int} p_send_now => ${True}
Execute SQL Store Procedure target_database=test_db_name target_store_procedure=example_package_name.example_procedure store_procedure_arguments=${example_procedure_argument_list}
我的错误如下:
[info (+0.10s)] 执行:调用存储过程 |示例程序包名称.示例程序 | ['p_item_no => 123456', 'p_send_now => True']
[FAIL] DatabaseError:ORA-06502:PL/SQL:数字或值错误:字符到数字转换错误 ORA-06512:位于第 1 行帮助:https://docs.oracle.com/error-help/db/ ora-06502/
自然我在离开Robot Framework时一直在努力确保我的数据是正确的类型,在阅读Robot Framework上的文档时:
DatabaseLibrary.Call Stored Procedure
关键字,我看到:
def call_stored_procedure(
self, spName: str, spParams: Optional[List[str]] = None, sansTran: bool = False, alias: Optional[str] = None
):
附说明:
使用
-spName
列表调用存储过程spParams
程序所需的参数。
关键字
DatabaseLibrary.Call Stored Procedure
/ spParams: Optional[List[str]]
有可能最终没有保留预期的数据类型吗?或者我可能还缺少其他东西?
我正在跑步:
robotframework>=7.0.0
robotframework-databaselibrary>=1.4.3
oracledb>=2.1.0
所以我对Robot Framework没有什么运气,而是不得不编写一个Python解决方案,如下所示:
from robot.api.deco import keyword
import oracledb
@keyword("Execute Stored Procedure")
def execute_stored_procedure(connection_details, procedure_name, arguments_dictionary):
"""
Executes a stored procedure on an Oracle database using the provided connection details,
procedure name, and arguments dictionary.
Args:
connection_details (dict): A dictionary containing connection details for the Oracle database.
Requires the following keys: 'dbUser', 'dbPassword', 'dbHost', 'dbPort', 'dbServiceName'.
procedure_name (str): The name of the stored procedure to execute.
arguments_dictionary (dict): A dictionary containing the arguments to pass to the stored procedure.
Returns:
None: This function does not return any value.
Raises:
oracledb.Error: If any error occurs during the execution of the stored procedure,
an oracledb.Error is raised.
Example:
| ${connection_details}= | Create Dictionary | dbUser | my_user | dbPassword | my_password | dbHost | localhost | dbPort | 1521 | dbServiceName | my_service |
| ${arguments}= | Create Dictionary | arg1 | value1 | arg2 | value2 |
| Execute Stored Procedure | ${connection_details} | my_stored_procedure | ${arguments} |
"""
# Construct the connection string
connection_string = f"{connection_details['dbUser']}/{connection_details['dbPassword']}@{connection_details['dbHost']}:{connection_details['dbPort']}/{connection_details['dbServiceName']}"
# Connect to the Oracle database
connection = oracledb.connect(connection_string)
try:
# Create a cursor
cursor = connection.cursor()
# Call the stored procedure with the provided name and arguments
cursor.callproc(procedure_name, keywordParameters=arguments_dictionary)
# Commit the transaction
connection.commit()
# Close the cursor
cursor.close()
except oracledb.Error as error:
print("Error occurred:", error)
# Rollback the transaction in case of error
connection.rollback()
finally:
# Close the connection
connection.close()