0

我计划使用 OCI Python SDK 在 OCI 上使用 Oracle NoSQL 云服务。我使用 API 调用做了一些测试,我想做同样的查询,但使用 Python SDK。

如何使用准备好的语句而不是常规语句(见下文)以及我需要设置哪些值

print("second attempt")
table_name = 'table1'
statement = 'declare $id integer; select * from ' + table_name + ' where id=$id'
id_value = '1'
#nosqlClient.prepare_statement(compartment_id=comp_ocid,statement=statement),
query_response = nosqlClient.query(
    query_details=oci.nosql.models.QueryDetails(
        compartment_id=comp_ocid,
        statement=statement,
        is_prepared=True,
        consistency="EVENTUAL",
        variables={
            '$id': id_value}))# Get the data from response
print(query_response.data) 

目前我有以下错误:

oci.exceptions.ServiceError: 
{'opc-request-id': '94E0B7EA0C864B379D66ED1C5215652A',
 'code': 'InvalidParameter', 
'message': 'QUERY: Illegal Argument: Deserialize prep query failed: Hash provided for prepared query does not match payload', 'status': 400}
4

1 回答 1

0

问题出在 QueryDetails 的“语句”属性上。

  1. 使用结果nosqlClient.prepare_statement(注意脚本末尾的逗号)
  2. 使用以下参数调用 oci.nosql.models.QueryDetailsstatement=prepare_statement_response.data.statement, is_prepared=True and variables
table_name = 'table1'
statement = 'declare $Id long; select * from ' + table_name + ' where id = $Id'
id_value = 1
prepare_statement_response = nosqlClient.prepare_statement(compartment_id=comp_ocid,statement=statement)
print(prepare_statement_response.data.statement)
query_response = nosqlClient.query(
    query_details=oci.nosql.models.QueryDetails(
        compartment_id=comp_ocid,
        statement=prepare_statement_response.data.statement,
        is_prepared=True,
        consistency="EVENTUAL",
        variables={
            '$Id': id_value}))
print(query_response.data)

希望这可以帮助你

于 2021-06-09T10:48:00.683 回答